生产者-消费者问题

生产者-消费者问题
1.一个多线程同步问题的经典案例
2.该问题描述了两个共享固定大小缓冲区的线程——即所谓的“生产者”和“消费者”——在实际运行时会发生的问题。
3.生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程。
4.与此同时,消费者也在缓冲区消耗这些数据。
5.该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据。

一组生产者进程和一组消费者进程共享一个初始为空、大小为n的缓冲区,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必须等待;只有缓冲区不空时,消费者才能从中取出消息,否则必须等待。由于缓冲区是临界资源,它只允许一个生产者放入消息,或者一个消费者取出消息
信号量设置:信号量mutex作为互斥信号量,它用于控制互斥访问缓冲池,互斥信号量初值为1;信号量full用于记录当前缓冲区中“满”缓冲区数,初值为0。信号量empty用于记录当前缓冲池中“空”缓冲区数,初值为n
semaphore mutex=1;//临界区互斥信号量
semaphore empty=n;//空闲缓冲区
semaphore full=0;//缓冲区初始化为空
producer()
{
while(1)
{
producer an item nextp;//生产数据
p(empty);//获取空缓冲区单元
p(mutex);//进入临界区
add nextp to buffer;//将数据放入缓冲区
v(mutex);//离开临界区,释放互斥信号量
v(full);//满缓冲区数加1
}
}
consumer()//消费者进程
{
while(1)
{
p(full);//获取满缓冲区单元
p(mutex);//进入临界区
remove an item from buffer;//从缓冲区中取出数据
V(mutex);//离开临界区,释放互斥信号量
V(empty);//空缓冲区数加1
consume the item;//消费数据
}
}


//oo-pc
//MutexLock.h
#ifndef __WD_MUTEXLOCK_H__
#define __WD_MUTEXLOCK_H__
#include"Noncopyable.h"
#include<pthread.h>
namespace wd
{
class MutexLock
: Noncopyable
{
public:
MutexLock();
~MutexLock();
void lock();
void unlock();
pthrerad_mutex_t * getMutexLockPtr();
private:
pthread_mutex_t _mutex;
bool _isLocking;
};
}//end of namespace wd
#endif

//MutexLock.cc
#include"MutexLock.h"
namespace wd
{
MutexLock::MutexLock()
: _isLocking(false)
{
pthread_mutex_init(&_mutex,NULL);
}
MutexLock::~MutexLock()
{
pthread_mutex_destroy(&_mutex);
}
void MutexLock::lock()
{
_isLocking=true;
pthread_mutex_lock(&_mutex);
}
void MutexLock::unlock()
{
pthread_mutex_unlock(&_mutex);
_isLocking=false;
}
pthread_mutex_t * MutexLock::getMutexLockPtr()
{
return &_mutex;
}
}//end of namespace wd

//Condition.h
#ifndef __WD_CONDITION _H__
#define __WD_CONDITION_H__
#include "Noncopuyable.h"
#include<pthread>
namespace wd
{
class MutexLock;//前向声明
class Condition
: Noncopyable
{
public:
Condition(Mutex & mutex);
~Condition();

void wait();
void notify();
void notifyall();
private:
pthread_cond_t _cond;
MutexLock & _mutex;
};
}//end of namespace wd
#endif

//Condition.cc
#include"Condition.h"
#include"MutexLock.h"
namespace wd
{
Condition::Condition(MutexLock & mutex)
: _mutex(mutex)
{
pthread_cond_init(&_cond,NULL);
}
void Condition::wait()
{
pthread_cond_wait(&_cond,_mutex.getMutexLockPtr());
}
void Condition::notify()
{
pthread_cond_signal(&_cond);
}
void Condition::notifyall()
{
pthread_cond_broadcast(&)
}
}

//Buffer.h
#ifndef __WD_BUFFER_H__
#define __WD_BUFFER_H__
#include "MutexLock.h"
#include "Condition.h"
#include <queue>
namespace wd
{
class Buffer
{
public:
Buffer(size_t size);
void push(int);
int pop();
bool empty();
bool full();
private:
MutexLock _mutex;
Condition _notFull;
Condition _notEmpty;
size_t _size;
std::queue<int> _que;
};
}//end of namespace wd

//Buffer.cc
#include"Buffer.h"
#include<iostream>
using std::cout;
using std::endl;
namespace wd
{
Buffer::Buffer(size_t size)
: _mutex()
, _notFull(_mutex)
, _notEmpty(_mutex)
, _size(size)
{
}
bool Buffer::empty()
{
return _que.size()==0;
}
bool Buffer::full()
{
return _que.size()==_size;
}
//push方法运行在生产者线程
void Buffer::push(int number)
{
_mutex.lock();
while(full())
_notfull.wait();//等待条件变量;使用while可以避免条件变量被异常唤醒
_que.push(number);
_notEmpty.notify();
_mutex.unlock();
}
//pop方法运行在消费者线程
int Buffer::pop()
{
_mutex.lock();
while(empty());
_notempty.wait();
int ret =_que.front();
_que.pop();
_notFull.notify();
_mutex.unlock();
return ret;
}
}//end of namespace wd

//Producer.h
#ifndef __WD_PRODUCER_H__
#define __WD_PRODUCER_H__
#include"Thread.h"
namespace wd
{
class Buffer;
class Producer:public Thread
{
public:
Prodecer(Buffer &);
virtual void run();
private:
Buffer & _buf;
};
}//end of namespace wd
#endif
//Producer.h
#ifndef __WD_PRODUCER_H__
#define __WD_PRODUCER_H__
#include "Thread.h"
namespace wd
{
class Buffer;
class Producer : public Thread
{
public:
Producer(Buffer &);
virtual void run();
private:
Buffer & _buf;
};
}//end of namespace wd
#endif

//Producer.cc
#include"Producer.h"
#include"Buffer.h"
#include <unistd.h>
#include <stdlib.h>
#include<time.h>
#include<iostream>
using std::cout;
using std::endl;
namespace wd
{
Producer::Producer(Buffer & buf)
: _buf(buf)
{}
void Producer::run()
{
::srand(::time(NULL));
while(1)
{
int number=::rand()%100;
_buf.push(number);
cout<<"Produce a number:"<<number<<endl;
sleep(2);
}
}
}//end of namespace wd


//Consumer.h
#ifndef __WD_CONSUMER_H__
#define __WD_CONSUMER_H__
#include"Thread.h"
namespace wd
{
class Buffer;
class Consumer : public Thread
{
public:
Consumer(Buffer & buf);
vitual void run();
private:
Buffer & _buf;
};
}//end of namespace wd
#endif

//Consumer.cc
#include"Consumer.h"
#include"Buffer.h"
#include<unistd.h>
#include<iostream>
using std::cout;
using std::endl;
namespace wd
{
Consumer::Comsumer(Buffer & buf)
: _buf(buf)
{}
void Consumer::run()
{
while(1)
[
int number=_buf.pop();
cout<<"-----consume a number: "<<number<<endl;
}
}
}//end of namespace wd

//Thread.h
#ifndef __WD_THREAD_H__
#define __WD_THREAD_H__
#include "Noncopyable.h"
#include <pthread.h>
namespace wd
{
class Thread
: private Noncopyable// 实现继承
//: public Noncopyable//接口继承
{
public:
Thread();
virtual ~Thread();
virtual void run()=0;
void start();
void join();
static void * threadFunc(void * arg);
private:
pthread_t _pthId;
bool _isRunning;
};
}//end of namespace wd
#endif

//Thread.cc
#include "Thread.h"
#include <iostream>
using std::cout;
using std::endl;
namespace wd
{
Thread::Thread()
: _pthId(0)
, _isRunning(false)
{}
void Thread::start()
{
pthread_create(&_pthId, NULL, threadFunc, this);
_isRunning = true;
}
void Thread::join()
{
if(_isRunning)
{
pthread_join(_pthId, NULL);
_isRunning = false;
}
}
void * Thread::threadFunc(void * arg)
{
Thread * p = static_cast<Thread*>(arg);
if(p)
p->run();
return NULL;
}
Thread::~Thread()
{
if(_isRunning)
{
pthread_detach(_pthId);
}
}
}//end of namespace wd



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值