参考网址:

了解boost

C++ Boost库分类总结(个人收藏) - 知乎 (zhihu.com)

boost教程

Highscore - Boost C++ 库 - 多线程

boost多线程同步简单使用

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
#include <boost/thread.hpp> 
#include <iostream>

void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

boost::mutex mutex;

void thread()
{
for (int i = 0; i < 5; ++i)
{
wait(1);
mutex.lock();
std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
mutex.unlock();
}
}

int main()
{
boost::thread t1(thread);
boost::thread t2(thread);
t1.join();
t2.join();
}