boost thread(1)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

想自己试试thread好用不好用。这是boost的一大特色。如果加到standard C++中的话,那么确实能够提高效率。

晚上调试thread的时候刚开始遇到一个问题

告诉我boost_thread-vc71-mt-gd-1_31.dll找不到。这个问题是这样的,因为在jam的时候只是将release版本的dll拷到了windows/systems32下面去了,没有将debug版本的文件拷过去。所以自己手工将文件拷过去。害的我以为我安装的有问题。让我重新装了一遍。

从最简单的开始:

#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[])

{

//创建一个thread 这个部分是创建完以后立马回到主进程,让线程去跑就是了。

//这个部分和MPI多机并行环境下的程序设计几乎是一样的。只不过一个是单机上的并行

//一个是分布式环境下的并行。

boost::thread thrd(&hello);

thrd.join();

//等待线程返回

return 0;

}

使用mutex互斥量

#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;

}

更加复杂一些,就是加了一个bind要比上面的代码好的地方在于不必使用functor

#include <boost/thread/thread.hpp>

#include <boost/thread/mutex.hpp>

#include <boost/bind.hpp>

#include <iostream>

boost::mutex io_mutex;

void count(int id)

{

for (int i = 0; i < 10; ++i)

{

boost::mutex::scoped_lock

lock(io_mutex);

std::cout << id << ": " <<

i << std::endl;

}

}

int main(int argc, char* argv[])

{

boost::thread thrd1(

boost::bind(&count, 1));

boost::thread thrd2(

boost::bind(&count, 2));

thrd1.join();

thrd2.join();

return 0;

}

boost::condition

这个就更加复杂一些

条件变量一般在 mutex 和shared resource的组合中使用.一个进程首先锁定 mutex 然后判断共享资源是不是在可以使用的状态。如果不在那么他就开始等conditional variable.这个操作使得线程在等待的时候 mutex被unlocked 。这样另外一个线程就可以改变共享资源的状态了。这里只要保证在使用共享资源时他的 mutex 是被锁定的就可以了。 当某些进程修改了这样的共享资源时,需要通知一下其他在等待的进程。这样其他进程就可以对出wait状态了。

#include <boost/thread/thread.hpp>

#include <boost/thread/mutex.hpp>

#include <boost/thread/condition.hpp>

#include <iostream>

const int BUF_SIZE = 10;

const int ITERS = 100;

boost::mutex io_mutex;

class buffer

{

public:

typedef boost::mutex::scoped_lock

scoped_lock;

buffer()

: p(0), c(0), full(0)

{

}

void put(int m)

{

scoped_lock lock(mutex);

if (full == BUF_SIZE)

{

{

boost::mutex::scoped_lock

lock(io_mutex);

std::cout <<

"Buffer is full. Waiting..."

<< std::endl;

}

while (full == BUF_SIZE)

cond.wait(lock);

}

buf[p] = m;

p = (p+1) % BUF_SIZE;

++full;

cond.notify_one();

}

int get()

{

scoped_lock lk(mutex);

if (full == 0)

{

{

boost::mutex::scoped_lock

lock(io_mutex);

std::cout <<

"Buffer is empty. Waiting..."

<< std::endl;

}

while (full == 0)

cond.wait(lk);

}

int i = buf[c];

c = (c+1) % BUF_SIZE;

--full;

cond.notify_one();

return i;

}

private:

boost::mutex mutex;

boost::condition cond;

unsigned int p, c, full;

int buf[BUF_SIZE];

};

buffer buf;

void writer()

{

for (int n = 0; n < ITERS; ++n)

{

{

boost::mutex::scoped_lock

lock(io_mutex);

std::cout << "sending: "

<< n << std::endl;

}

buf.put(n);

}

}

void reader()

{

for (int x = 0; x < ITERS; ++x)

{

int n = buf.get();

{

boost::mutex::scoped_lock

lock(io_mutex);

std::cout << "received: "

<< n << std::endl;

}

}

}

int main(int argc, char* argv[])

{

boost::thread thrd1(&reader);

boost::thread thrd2(&writer);

thrd1.join();

thrd2.join();

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航