boost学习之-生产者消费者问题例子
2012-07-21 23:36
295 查看
生产者消费者问题在线程编程中是基础问题,很重要,很多其他问题的解决都通过扩展该问题的解决方法来解决的;
boost中的一个例子是这样写的,很简洁;所以boost很强大,屏蔽掉了很多繁琐的问题;
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <vector>
class bounded_buffer : private boost::noncopyable
{
public:
typedef boost::mutex::scoped_lock lock;
bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }
void send (int m) {
lock lk(monitor);
while (buffered == circular_buf.size())
buffer_not_full.wait(lk);
circular_buf[end] = m;
end = (end+1) % circular_buf.size();
++buffered;
buffer_not_empty.notify_one();
}
int receive() {
lock lk(monitor);
while (buffered == 0)
buffer_not_empty.wait(lk);
int i = circular_buf[begin];
begin = (begin+1) % circular_buf.size();
--buffered;
buffer_not_full.notify_one();
return i;
}
private:
int begin, end, buffered;
std::vector<int> circular_buf;
boost::condition buffer_not_full, buffer_not_empty;
boost::mutex monitor;
};
bounded_buffer buf(2);
void sender() {
int n = 0;
while (n < 100) {
buf.send(n);
std::cout << "sent: " << n << std::endl;
++n;
}
buf.send(-1);
}
void receiver() {
int n;
do {
n = buf.receive();
std::cout << "received: " << n << std::endl;
} while (n != -1); // -1 indicates end of buffer
}
int main()
{
boost::thread thrd1(&sender);
boost::thread thrd2(&receiver);
thrd1.join();
thrd2.join();
}
读例子是很好的学习方式!
boost中的一个例子是这样写的,很简洁;所以boost很强大,屏蔽掉了很多繁琐的问题;
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <vector>
class bounded_buffer : private boost::noncopyable
{
public:
typedef boost::mutex::scoped_lock lock;
bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }
void send (int m) {
lock lk(monitor);
while (buffered == circular_buf.size())
buffer_not_full.wait(lk);
circular_buf[end] = m;
end = (end+1) % circular_buf.size();
++buffered;
buffer_not_empty.notify_one();
}
int receive() {
lock lk(monitor);
while (buffered == 0)
buffer_not_empty.wait(lk);
int i = circular_buf[begin];
begin = (begin+1) % circular_buf.size();
--buffered;
buffer_not_full.notify_one();
return i;
}
private:
int begin, end, buffered;
std::vector<int> circular_buf;
boost::condition buffer_not_full, buffer_not_empty;
boost::mutex monitor;
};
bounded_buffer buf(2);
void sender() {
int n = 0;
while (n < 100) {
buf.send(n);
std::cout << "sent: " << n << std::endl;
++n;
}
buf.send(-1);
}
void receiver() {
int n;
do {
n = buf.receive();
std::cout << "received: " << n << std::endl;
} while (n != -1); // -1 indicates end of buffer
}
int main()
{
boost::thread thrd1(&sender);
boost::thread thrd2(&receiver);
thrd1.join();
thrd2.join();
}
读例子是很好的学习方式!
相关文章推荐
- C++学习 boost学习之-生产者消费者问题例子
- apue学习第二十四天——高级I/O、生产者消费者问题、socket(提纲)
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- boost c++ lib on linux(4) - thread同步条件变量学习——生产者消费者队列
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- 进一步学习PV操作——统一于生产者消费者问题
- Java 线程同步问题 生产者-消费者 算法实现 -Java学习笔记(29)
- 马士兵-多线程学习第05课 生产者消费者问题
- 学习和理解JAVA线程同步--生产者与消费者例子
- Linux C/C++多线程学习:生产者消费者问题
- 生产者和消费者问题学习以及Java实现
- 学习和理解JAVA线程同步--生产者与消费者例子
- java 多线程学习之多生产者多消费者产生的线程安全问题分析与解决:Lock和Condition
- Java多线程学习之生产者消费者问题(一)
- java中的多生产者-消费者问题学习
- java 多线程经典例子——生产者与消费者的问题
- 并发编程(二):分析Boost对 互斥量和条件变量的封装及实现生产者消费者问题
- 并发编程(二):分析Boost对 互斥量和条件变量的封装及实现生产者消费者问题
- JAVA多线程学习--生产者消费者问题
- 操作系统学习笔记-信号量及PV操作与进程互斥和消费者生产者问题