Line | |
---|
1 | // Copyright (C) 2001-2003 |
---|
2 | // William E. Kempf |
---|
3 | // |
---|
4 | // Permission to use, copy, modify, distribute and sell this software |
---|
5 | // and its documentation for any purpose is hereby granted without fee, |
---|
6 | // provided that the above copyright notice appear in all copies and |
---|
7 | // that both that copyright notice and this permission notice appear |
---|
8 | // in supporting documentation. William E. Kempf makes no representations |
---|
9 | // about the suitability of this software for any purpose. |
---|
10 | // It is provided "as is" without express or implied warranty. |
---|
11 | |
---|
12 | #include <boost/thread/thread.hpp> |
---|
13 | #include <boost/thread/tss.hpp> |
---|
14 | #include <cassert> |
---|
15 | |
---|
16 | boost::thread_specific_ptr<int> value; |
---|
17 | |
---|
18 | void increment() |
---|
19 | { |
---|
20 | int* p = value.get(); |
---|
21 | ++*p; |
---|
22 | } |
---|
23 | |
---|
24 | void thread_proc() |
---|
25 | { |
---|
26 | value.reset(new int(0)); // initialize the thread's storage |
---|
27 | for (int i=0; i<10; ++i) |
---|
28 | { |
---|
29 | increment(); |
---|
30 | int* p = value.get(); |
---|
31 | assert(*p == i+1); |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | int main(int argc, char* argv[]) |
---|
36 | { |
---|
37 | boost::thread_group threads; |
---|
38 | for (int i=0; i<5; ++i) |
---|
39 | threads.create_thread(&thread_proc); |
---|
40 | threads.join_all(); |
---|
41 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.