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