分享
 
 
 

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有