BOOST线程详解
线程的中断点
thread::join | myThread::join调用这个方法的线程进入wait状态,直到myThread代表的线程完成 |
thread::try_join_for thread::try_join_until | 阻塞等待一定的时间段 |
condition_variable_any::wait | wait(mu) |
condition_variable_any::wait_for condition_variable_any::wait_until | |
condition_variable_any::notify_one condition_variable_any::notify_all | 通知一个/所有等待中的线程 |
this_thread::sleep_for this_thread::sleep_until | 当前线程放弃时间片,指定堵塞时间,允许其他同优先级的线程运行,不释放资源 |
this_thread::yield | 当前线程放弃时间片,不指定堵塞时间,允许其他同优先级的线程运行,不释放资源 |
thread::detach | 分离线程 |
thread::interrupt | 中断线程 |
互斥锁 | boost::mutex mu boost::mutex::scoped_lock lock(mu) |
读写锁 | boost::shared_mutex rw_mu boost::shared_lock<boost::shared_mutex> lock(rw_mu) boost::unique_lock<boost::shared_mutex> lock(rw_mu) |
条件量 | boost::mutex ioMu boost::condition_variable_any condPut boost::mutex::scoped_lock lock(ioMu) condPut.wait(ioMu) condPut.notify_one() |
递归锁 | boost::recursive_mutex rm boost::recursive_mutex::scoped_lock lock(rw) 或者 boost::lock_guard<boost::recursive_mutex> lock(rw) |
lambda表达式:匿名函数,自动类型推断和类型获取
[captures] (params) –> ret {Statements;} 函数返回值后置
atomic原子操作
lock_guard、shared_lock、unique_lock都是RAII(资源获取就是初始化)
boost::mutex::scoped_lock是boost::unique_lock<boost::mutex>的一个子类
互斥锁:
int g_num = 0;
boost::mutex mu; //定义互斥锁对象
int Func(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::mutex::scoped_lock lock(mu); //对共享数据进行操作,需加锁
g_num++;
//cout << __FUNCTION__ << ": " << g_num << endl;
cout << boost::this_thread::get_id() << ":" << g_num << endl;
}
return g_num;
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(Func, 100);
boost::thread th2(Func, 200);
th1.join();
th2.join();
cout << boost::this_thread::get_id() << ":" << "main_end" << endl;
//th1和th2的执行顺序不定,不过一定是一个执行完毕后另一个才会执行,最后才是main_end
system("pause");
return 0;
}
读写锁:
int g_num = 0;
boost::shared_mutex rw_mu; //定义读写锁
int Write(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::unique_lock<boost::shared_mutex> lock(rw_mu); //加唯一锁
g_num++;
//cout << __FUNCTION__ << ": " << g_num << endl;
cout << boost::this_thread::get_id() << ": " << __FUNCTION__ << ": " << g_num << endl;
}
return g_num;
}
void Read(int nCount)
{
for (int i = 0; i < nCount; i++)
{
boost::shared_lock<boost::shared_mutex> lock(rw_mu); //加共享锁
//cout << __FUNCTION__ << ": " << g_num << endl;
cout << boost::this_thread::get_id() << ": " << __FUNCTION__ << ": (" << i << "):" << g_num << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(Write, 100);
boost::thread th2(Read, 100);
boost::thread th3(Read, 100);
th1.join();
th2.join();
th3.join();
system("pause");
return 0;
}
条件量:
boost::mutex g_ioMutex; //输出控制锁
template<typename T>
class CMsgQueue
{
public:
CMsgQueue(size_t n):m_nCapacity(n)
{
}
void Push(const T& val)
{
{
boost::mutex::scoped_lock lock(m_mu); //加锁
while(m_val.size() == m_nCapacity) //队列已满
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << "队列已满" << endl;
}
m_condPush.wait(m_mu); //等待,将暂时的解锁
}
m_val.push(val); //添加数据到队列
}
m_condPop.notify_one(); //通知读线程
}
void Pop(T& val)
{
{
boost::mutex::scoped_lock lock(m_mu); //加锁
while(m_val.size() == 0) //队列为空
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << "队列为空" << endl;
}
m_condPop.wait(m_mu); //等待可读,
}
val = m_val.front(); //读取数据
m_val.pop();
}
m_condPush.notify_one(); //通知写线程
}
private:
queue<T> m_val; //队列
int m_nCapacity; //队列最大容量
boost::condition_variable_any m_condPush; //写入条件量
boost::condition_variable_any m_condPop; //读取条件量
boost::mutex m_mu; //互斥锁
};
CMsgQueue<int> g_numQueue(10);
void Push(int nCount)
{
for (int i = 0; i < nCount; i++)
{
{
boost::mutex::scoped_lock lock(g_ioMutex);
cout << boost::this_thread::get_id() << ":" << __FUNCTION__ << " :Before_Push :" << i << endl;
}
g_numQueue.Push(i);
}
}
void Pop(int nCount)
{
for (int i = 0; i < nCount; i++)
{
int val;
g_numQueue.Pop(val);
boost::mutex::scoped_lock lock(g_ioMutex);
cout << boost::this_thread::get_id() << ":" << __FUNCTION__ << " :After_Pop(" << i << "):" << val << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread th1(Push, 50);
boost::thread th2(Pop, 20);
boost::thread th3(Pop, 30);
th1.join();
th2.join();
th3.join();
system("pause");
return 0;
}
未完待续…