Ticket #13226: thread_tss_test.cpp

File thread_tss_test.cpp, 733 bytes (added by claus.klein@…, 5 years ago)

example to compile

Line 
1#include <boost/thread/tss.hpp> // tss -> tls: thread local storage
2#include <boost/thread.hpp>
3#include <iostream>
4
5boost::mutex mutex;
6
7void init()
8{
9 static boost::thread_specific_ptr<bool> tls;
10 if (!tls.get()) {
11 tls.reset(new bool(true));
12 boost::lock_guard<boost::mutex> lock(mutex);
13 std::cout << "done" << '\n';
14 } else if (*tls) {
15 *tls = false;
16 boost::lock_guard<boost::mutex> lock(mutex);
17 std::cout << "set to false" << '\n';
18 }
19}
20
21void task()
22{
23 init();
24 init(); // again ...
25}
26
27int main()
28{
29 boost::thread t[3];
30
31 for (int i = 0; i < 3; ++i) {
32 t[i] = boost::thread(task);
33 }
34
35 for (int i = 0; i < 3; ++i) {
36 t[i].join();
37 }
38}
39