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 | #include <boost/thread/thread.hpp> |
8 | #include <boost/thread/tss.hpp> |
9 | #include <cassert> |
10 | |
11 | boost::thread_specific_ptr<int> value; |
12 | |
13 | void increment() |
14 | { |
15 | int* p = value.get(); |
16 | ++*p; |
17 | } |
18 | |
19 | void thread_proc() |
20 | { |
21 | value.reset(new_value: new int(0)); // initialize the thread's storage |
22 | for (int i=0; i<10; ++i) |
23 | { |
24 | increment(); |
25 | int* p = value.get(); |
26 | assert(*p == i+1); |
27 | (void)(p); |
28 | } |
29 | } |
30 | |
31 | int main() |
32 | { |
33 | boost::thread_group threads; |
34 | for (int i=0; i<5; ++i) |
35 | threads.create_thread(threadfunc: &thread_proc); |
36 | threads.join_all(); |
37 | } |
38 |