boost::thread线程创建方式总结



首先看看boost::thread的构造函数吧,boost::thread有两个构造函数:
(1)thread():构造一个表示当前执行线程的线程对象;
(2)explicit thread(const boost::function0<void>& threadfunc):
     boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。
     这里的函数也可以是类重载operator()构成的函数;
     该构造函数传入的是函数对象而并非是函数指针,
     这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。
     
第一种方式:最简单方法
#include <boost/thread/thread.hpp>
#include <iostream>   
void hello()
{
        std::cout <<
        "Hello world, I''m a thread!"
        << std::endl;
}   
int main(int argc, char* argv[])
{
        boost::thread thrd(&hello);
        thrd.join();
        return 0;
}


第二种方式:复杂类型对象作为参数来创建线程:
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>   
boost::mutex io_mutex;   
struct count
{
        count(int id) : id(id) { }         
        void operator()()
        {
                for (int i = 0; i < 10; ++i)
                {
                        boost::mutex::scoped_lock
                        lock(io_mutex);
                        std::cout << id << ": " << i << std::endl;
                }
        }         
        int id;
};
int main(int argc, char* argv[])
{
        boost::thread thrd1(count(1));
        boost::thread thrd2(count(2));
        thrd1.join();
        thrd2.join();
        return 0;
}  


第三种方式:在类内部创建线程;
(1)类内部静态方法启动线程
#include <boost/thread/thread.hpp>
#include <iostream>
class HelloWorld
{
public:
static void hello()
{  
std::cout << "Hello world, I''m a thread!" << std::endl; 
}
static void start()
{
boost::thread thrd( hello );
thrd.join();
}
};


int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
}   //在这里start()和hello()方法都必须是static方法。




(2)如果要求start()和hello()方法不能是静态方法则采用下面的方法创建线程:
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
    std::cout << "Hello world, I''m a thread!" << std::endl;
}
void start()
{
boost::function0< void> f = boost::bind(&HelloWorld::hello,this);
boost::thread thrd( f );
thrd.join();
}
};
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}
---------------------------------------------------------------------


线程ID从boost::thread中分离出来,因此一个线程可以获得它自己的ID(使用boost::this_thread::get_id()),
并且这些ID可以用作关联容器的键值。


主线程,子线程 , 线程, 资源的问题,线程同步问题


1.创建线程
2.使资源是线程安全的
保证同一时刻多个线程不会同时修改同一个共享资源,那么这个程序是线程安全的。
可以使用mutex类来控制线程的并发问题。

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <string>


template<typename T>
class CQueue
{
public:
CQueue() {}
~CQueue() {}


void enqueue(const T& x)
{
boost::mutex::scoped_lock  lock(mutex_);
list_.push_back(x);
std::cout << "enqueue" << std::endl;
}


T dequeue()
{
boost::mutex::scoped_lock lock(mutex_);
if(list_.empty())
throw "empty!";
T tmp = list_.front();
list_.pop_front();
std::cout << "dequeue" << std::endl;
return tmp;
}
private:
std::list<T> list_;
boost::mutex mutex_;
};


CQueue<std::string> queue_string;


void send_something()
{
for(int i=0; i<4; ++i)
queue_string.enqueue("Cyrus");
}


void recv_something()
{
std::string s;
for(int i=0; i<4; ++i)
{
try
{
s = queue_string.dequeue();
}
catch(...){}
}
}


int main(int argc, char* argv[])
{
boost::thread thrd1(send_something);
boost::thread thrd2(recv_something);


thrd1.join();
thrd2.join();


return 0;
}


在某一时刻,只有一个线程可以锁定这个mutex对象。
这就阻止了同一时刻多个线程并发访问共享资源。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值