关于 boost 多线程(一)
在纯 C++ 开发中,线程的创建由 pthread_create 函数实现。成功返回0,出错返回 -1.其函数原型为:
int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);
调用该函数时需要传入一个函数地址作为形参,该地址即为线程运行的入口。从创建的过程中可以看到,创建的同时绑定了线程任务(指定函数入口地址)。接下来只要 run 起来就可以了。对于多线程来说,boost 中有专门用来管理多个线程的 thread_group 类,它的作用相当于线程池。在 thread_group 的内部机制中,用 std::list <thread *>
来实现多个线程的管理。查看boost库发现,thread_group 类的定义:
class thread_group
{
private:
thread_group(thread_group const&);
thread_group& operator=(thread_group const&);
public:
thread_group() {}
~thread_group()
{
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
delete *it;
}
}
bool is_this_thread_in()
{
thread::id id = this_thread::get_id();
boost::shared_lock<shared_mutex> guard(m);
for(std::list<thread*>: