boost
更新日期:
#asio
asio::io_service(io_context)
- 任务池
在该任务池注册过的线程可到池里取任务执行
同个类实例的同个函数可同时执行
asio::strand
- 同个类实例的同个函数互斥执行
- 不同类实例的同个函数可同时执行
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
41void func2(char a) {
for (int i = 1; i < 50; i++)
std::cout << a << std::endl;
}
class T1 {
private:
boost::asio::strand strand;
public:
T1(boost::asio::io_service& svc) :strand(svc) {}
void func1(char b);
void run();
};
void T1::func1(char b) {
func2(b);
}
void T1::run() {
strand.post(boost::bind(&T1::func1, this, 'a'));
strand.post(boost::bind(&T1::func1, this, 'b'));
}
boost::asio::io_service ios;
// regist thread...
T1 t1(ios);
t1.run();
ios.run();
//loop
result:
a
a
a
...
a
b
b
b
...
b