thread_guard.cpp 886 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // (C) Copyright 2009-2012 Anthony Williams
  2. // (C) Copyright 2012 Vicente Botet
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <string>
  8. #include <boost/thread/thread_only.hpp>
  9. #include <boost/thread/thread_guard.hpp>
  10. void do_something(int& i)
  11. {
  12. ++i;
  13. }
  14. struct func
  15. {
  16. int& i;
  17. func(int& i_):i(i_){}
  18. void operator()()
  19. {
  20. for(unsigned j=0;j<1000000;++j)
  21. {
  22. do_something(i);
  23. }
  24. }
  25. private:
  26. func& operator=(func const&);
  27. };
  28. void do_something_in_current_thread()
  29. {}
  30. void f()
  31. {
  32. int some_local_state;
  33. func my_func(some_local_state);
  34. boost::thread t(my_func);
  35. boost::thread_guard<> g(t);
  36. do_something_in_current_thread();
  37. }
  38. int main()
  39. {
  40. f();
  41. return 0;
  42. }