boost库使用—线程类
boost 库中提供了两种创建线程的方式:
一种是单个线程创建,另外一种是线程组的创建,进行线程管理;同时,在线程库中还提供了锁的方式;
thread 线程
thread 就是没有组管理,与我们在linux下使用pthread_create()函数是一样的,只是在C++11中,引入了boost中的thread方法;
包含头文件:
#include
using namespace boost;
常用方法:thread th(...) 创建线程
th.interrupt() 中断线程
th.get_id() 获取线程ID
th.join() 等待线程结束
案例:
#include
#include
#include
#include
using namespace std;
boost::mutex io_mutex;
void Print(int x, string str)
try
{
for (int i = 0; i < x; i++)
{
boost::mutex::scoped_lock lock(io_mutex);
cout << "str:"<
}
}
catch (boost::thread_interrupted&)
{