您的位置:首页 > 其它

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

2015-07-03 08:45 330 查看
首先看看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对象。

这就阻止了同一时刻多个线程并发访问共享资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: