tss.cpp 762 B

12345678910111213141516171819202122232425262728293031323334353637
  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/tss.hpp>
  8. #include <cassert>
  9. boost::thread_specific_ptr<int> value;
  10. void increment()
  11. {
  12. int* p = value.get();
  13. ++*p;
  14. }
  15. void thread_proc()
  16. {
  17. value.reset(new int(0)); // initialize the thread's storage
  18. for (int i=0; i<10; ++i)
  19. {
  20. increment();
  21. int* p = value.get();
  22. assert(*p == i+1);
  23. (void)(p);
  24. }
  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. }