-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathb-thread.cpp
More file actions
51 lines (40 loc) · 1.27 KB
/
b-thread.cpp
File metadata and controls
51 lines (40 loc) · 1.27 KB
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
45
46
47
48
49
50
51
#include "study.hpp"
#include <boost/thread.hpp>
boost::recursive_mutex mtx;
void locky(int i)
{
std::cout << "start lock " << i << "\n";
if (i > 0)
{
boost::recursive_mutex::scoped_lock sl(mtx);
usleep(100 * 1000);
locky(i-1);
}
usleep(100*1000);
std::cout << "finish lock " << i << "\n";
}
template <typename Mutex>
void lockwhile(boost::function<void()> h, Mutex& m)
{
typename Mutex::scoped_lock lock(m);
h();
}
int main(int, char**)
{
SHOW();
boost::function<void()> fn = boost::bind(&locky, 10);
boost::thread th(boost::bind(&lockwhile<boost::recursive_mutex>, fn, boost::ref(mtx)));
usleep(10000);
boost::recursive_mutex::scoped_lock triedlock(mtx, boost::defer_lock);
std::cout << "try lock = " << triedlock.try_lock() << "\n";
std::cout << "try lock = " << triedlock.try_lock() << "\n";
std::cout << "try lock = " << triedlock.try_lock() << "\n";
std::cout << "owns lock = " << triedlock.owns_lock() << "\n";
std::cout << "get lock in other thread\n";
boost::recursive_mutex::scoped_lock otherlock(mtx);
std::cout << "GOT\ntrying again...\n";
std::cout << "try lock = " << triedlock.try_lock() << "\n";
std::cout << "owns lock = " << triedlock.owns_lock() << "\n";
std::cout << "wait and join\n";
th.join();
}