今天学习了一下boost thread库的使用,只是粗浅的学习了怎么使用boost thread的库函数构建多线程的c++程序,了解了基本的thread创建,线程同步以及线程本地存储的使用。都在一个代码实例中进行了 验证。其中还有一个条件变量的使用在不好在一个代码中实现,以后有时间在编写验证一下。
源代码
/*************************************************************************
> File Name: thread_demo.cpp
> Author: Liu Xin
> Mail: liu_x_0625@126.com
> Created Time: 2012年10月14日 星期日 10时54分46秒
************************************************************************/
#include<iostream>
#include<sstream>
#include<boost/thread/thread.hpp>
#include<boost/thread/mutex.hpp>
#include<boost/bind.hpp>
#include<boost/thread/tss.hpp>
using namespace std;
const int MAX_THREAD_NUM=5;
int gCount=0;
boost::mutex mutex;
boost::mutex io_mutex;
boost::thread_specific_ptr<string> ptr; // 线程本地存储访问
void hello(int iThreadId)
{
// 这两个cout语句在多线程的时候还是汇出现io争用的情况,出现打印混乱, 所以要加锁
{
boost::mutex::scoped_lock lock(io_mutex);
std::

本文介绍了如何使用Boost Thread库创建和管理C++多线程程序,涵盖了线程创建、同步及线程局部存储的基本用法。通过一个实例代码进行了验证,同时提及了条件变量的后续学习计划。
最低0.47元/天 解锁文章
448

被折叠的 条评论
为什么被折叠?



