复习使用
//=================================================================================
// BlockQueue.hpp
#include <iostream>
#include <pthread.h>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <fcntl.h>
#include <queue>
using namespace std;
//************************************************
//创建自动锁->自动上锁,自动释放锁
class Auto_lock
{
public:
Auto_lock(pthread_mutex_t* reMax)
:_AMax(reMax)
{
pthread_mutex_lock(_AMax);
}
~Auto_lock()
{
pthread_mutex_unlock(_AMax);
}
private:
pthread_mutex_t* _AMax;
};
//************************************************
//设置等待与唤醒函数
void wait(pthread_cond_t*cond,pthread_mutex_t* mtx)
{
pthread_cond_wait(cond,mtx);
}
void weekup(pthread_cond_t*cond)
{
pthread_cond_signal(cond);
}
//************************************************
//建立临界资源与临界区代码
template <class Ty>
class BQueue
{
//检测bq空与满
bool empty()
{
return _bq.empty();
}
bool full()
{
return _bq.size()==_capacity;
}
public:
BQueue(size_t capacity=5)
: _capacity(capacity)
{
//初始化条件变量
_Full=new pthread_cond_t;
_Empty=new pthread_cond_t;
_mtx=new pthread_mutex_t;
pthread_mutex_init(_mtx,nullptr);
pthread_cond_init(_Empty,nullptr);
pthread_cond_init(_Full,nullptr);
}
void push(const Ty&x)
{
{ //放入数据
Auto_lock am(_mtx);
while(full()) wait(_Full,_mtx);
_bq.push(x);
weekup(_Empty);
}
}
void pop(Ty*x)
{
{ //取数据
Auto_lock am(_mtx);
while(empty()) wait(_Empty,_mtx);
*x=_bq.front();
_bq.pop();
weekup(_Full);
}
}
~BQueue()
{
pthread_mutex_destroy(_mtx);
pthread_cond_destroy(_Empty);
pthread_cond_destroy(_Full);
delete _mtx;
delete _Empty;
delete _Full;
}
private:
queue<Ty> _bq;
size_t _capacity;
pthread_cond_t* _Full;
pthread_cond_t* _Empty;
pthread_mutex_t* _mtx;
};
//==================================================================================
// teskblock.hpp
#include <iostream>
#include <functional>
using namespace std;
void Task_1()
{
cout<<"执行: Sql任务"<<endl;
}
void Task_2()
{
cout<<"执行: 全盘扫描任务"<<endl;
}
void Task_3()
{
cout<<"执行: 临时文件清除任务"<<endl;
}
void Task_4()
{
cout<<"执行: 网络接收数据任务"<<endl;
}
typedef function<void ()> func_t;
//==================================================================================
// main.cc
#include"teskblock.hpp"
#include"BlockQueue.hpp"
func_t Tesks[]={Task_1,Task_2,Task_3,Task_4};
//************************************************
//生产者线程运行
void*productor(void*ags)
{
pthread_detach(pthread_self());
BQueue<func_t>*bq=(BQueue<func_t>*)ags;
while(1)
{
bq->push(Tesks[rand()%4]);
cout<<" 生产者:"<<pthread_self()<<endl;
sleep(1);
}
}
//************************************************
//消费者线程运行
void*consumer(void*ags)
{
pthread_detach(pthread_self());
BQueue<func_t>*bq=(BQueue<func_t>*)ags;
while(1)
{
func_t ret;
bq->pop(&ret);
cout<<" 消费者:"<<pthread_self()<<" ";
ret();
}
}
//************************************************
int main()
{
srand((unsigned int)time(0));
BQueue<func_t> bq(5);
pthread_t c1,c2,p1,p2;
pthread_create(&c1,nullptr,consumer,(void*)&bq);
pthread_create(&c2,nullptr,consumer,(void*)&bq);
pthread_create(&p1,nullptr,productor,(void*)&bq);
pthread_create(&p2,nullptr,productor,(void*)&bq);
while(1);
}