-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_tss_test.cpp
44 lines (37 loc) · 992 Bytes
/
thread_tss_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#undef _POSIX_C_SOURCE
#include <boost/assert.hpp>
#include <boost/thread.hpp>
#include <boost/thread/tss.hpp> // tss(Class thread specific storage) -> tls: thread local storage
#include <iostream>
boost::mutex
mutex; // warning: initialization of 'mutex' with static storage duration
// may throw an exception that cannot be caught [cert-err58-cpp]
void init()
{
static boost::thread_specific_ptr<bool> tls;
if (!tls.get()) {
tls.reset(new bool(true));
boost::lock_guard<boost::mutex> lock(mutex);
std::cout << "done" << '\n';
} else if (*tls) {
assert(*tls == true);
*tls = false;
boost::lock_guard<boost::mutex> lock(mutex);
std::cout << "set to false" << '\n';
}
}
void task()
{
init();
init(); // again ...
}
int main()
{
boost::thread t[3];
for (int i = 0; i < 3; ++i) {
t[i] = boost::thread(task);
}
for (int i = 0; i < 3; ++i) {
t[i].join();
}
}