once.cpp 830 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2001-2003
  2. // William E. Kempf
  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. #define BOOST_THREAD_PROVIDES_ONCE_CXX11
  7. #include <boost/thread/thread.hpp>
  8. #include <boost/thread/once.hpp>
  9. #include <cassert>
  10. int value=0;
  11. #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
  12. static boost::once_flag once;
  13. //static boost::once_flag once2 = BOOST_ONCE_INIT;
  14. #else
  15. static boost::once_flag once = BOOST_ONCE_INIT;
  16. //static boost::once_flag once2 = once;
  17. #endif
  18. void init()
  19. {
  20. ++value;
  21. }
  22. void thread_proc()
  23. {
  24. boost::call_once(&init, once);
  25. }
  26. int main()
  27. {
  28. boost::thread_group threads;
  29. for (int i=0; i<5; ++i)
  30. threads.create_thread(&thread_proc);
  31. threads.join_all();
  32. assert(value == 1);
  33. }