once.cpp 630 B

12345678910111213141516171819202122232425262728293031
  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. #include <boost/thread/thread.hpp>
  7. #include <boost/thread/once.hpp>
  8. #include <cassert>
  9. int value=0;
  10. boost::once_flag once = BOOST_ONCE_INIT;
  11. void init()
  12. {
  13. ++value;
  14. }
  15. void thread_proc()
  16. {
  17. boost::call_once(&init, once);
  18. }
  19. int main(int argc, char* argv[])
  20. {
  21. boost::thread_group threads;
  22. for (int i=0; i<5; ++i)
  23. threads.create_thread(&thread_proc);
  24. threads.join_all();
  25. assert(value == 1);
  26. }