轮询查看
#include<iostream>
#include"osapi/osapi.h"
using namespace std;
OS_Mutex g_mutex;
int g_Buffer[100];
int g_Count = 0;
class Producer : public OS_Thread
{
public:
bool quitFlag;
Producer() :quitFlag(true) {}
void start()
{
quitFlag = false;
Run();
}
void stop()
{
quitFlag = true;
Join(this);
}
private:
virtual int Routine()
{
while (!quitFlag)
{
int r = rand() % 20 + 1;//任意数对20取余
OS_Thread::Msleep(50);//每隔一段时间生成一个数,然后放入buffer中
//同时等待也为了防止程序执行到g_mutex.Lock()中,程序仍在被锁着,消费者那边获取不到锁,一直无法取走东西
g_mutex.Lock();
g_Buffer[g_Count] = r;
g_Count = g_Count + 1;
cout << g_Count-1 <<"放入:" << r <<"个物品" << endl;
g_mutex.Unlock();
}
return 0;
}
};
class Consumer : public OS_Thread
{
public:
bool quitFlag;
Consumer() :quitFlag(true) {}
void start()
{
quitFlag = false;
Run();
}
void stop()
{
quitFlag = true;
Join(this);
}
private:
virtual int Routine()
{
while (!quitFlag)
{
OS_Thread::Msleep(300);//给生产的时间长一点
g_mutex.Lock();
while(g_Count>0)//每隔一段时间去查询,看看有没有生产产品
{
g_Count = g_Count - 1;
cout << g_Count << "消费物品:" << g_Buffer[g_Count] << endl;
}
g_mutex.Unlock();
}
return 0;
}
};
int main()//主线程
{
Producer product;
product.start();
Consumer person;
person.start();
cout << "---main routine is running----" << endl;
getchar();
product.stop();
person.stop();
cout << "退出程序" << endl;
return 0;
}
轮询机制缺点:
查询太频繁浪费CPU,太不频繁,缓冲区溢出,最好有一个通知机制,生产者把物品放进去之后,通知消费者,消费者接到通知,及时去走
线程之间通知机制:信号量(和mutex一样),是一个系统级对象
OS_Semaphore g_sem;
第一个线程:Producer
g_sem.Post();//通知
第二个线程:Comsumer
g_sem.Wait();//等待通知
信号量的值:0,1,2...N
也就是说,生产者可以一次放入多个物品,并将信号量的值增加
g_sem.Post();//信号量的值加1
g_sem.Wait();//信号量的值减1,如果信号量的值为0,则线程进行等待状态
#include<iostream>
#include"osapi/osapi.h"
using namespace std;
OS_Mutex g_mutex;
int g_Buffer[100];
int g_Count = 0;
OS_Semaphore g_sem(0);//信号量0
class Producer : public OS_Thread
{
public:
bool quitFlag;
Producer() :quitFlag(true) {}
void start()
{
quitFlag = false;
Run();
}
void stop()
{
quitFlag = true;
Join(this);
}
private:
virtual int Routine()
{
while (!quitFlag)
{
int r = rand() % 20 + 1;//任意数对20取余
OS_Thread::Msleep(50);//每隔一段时间生成一个数,然后放入buffer中
//同时等待也为了防止程序执行到g_mutex.Lock()中,程序仍在被锁着,消费者那边获取不到锁,一直无法取走东西
g_mutex.Lock();
g_Buffer[g_Count] = r;
g_Count = g_Count + 1;
cout << g_Count-1 <<"放入:" << r <<"个物品" << endl;
g_mutex.Unlock();
g_sem.Post();//把信号量内部的值加1
}
return 0;
}
};
class Consumer : public OS_Thread
{
public:
bool quitFlag;
Consumer() :quitFlag(true) {}
void start()
{
quitFlag = false;
Run();
}
void stop()
{
quitFlag = true;
Join(this);
}
private:
virtual int Routine()
{
while (!quitFlag)
{
g_sem.Wait();//如果信号量里面的值,为零,则等待,不为零,则向下执行,同时信号量减一
g_mutex.Lock();
while(g_Count>0)//每隔一段时间去查询,看看有没有生产产品
{
g_Count = g_Count - 1;
cout << g_Count << "消费物品:" << g_Buffer[g_Count] << endl;
}
g_mutex.Unlock();
}
return 0;
}
};
int main()//主线程
{
Producer product;
product.start();
Consumer person;
person.start();
cout << "---main routine is running----" << endl;
getchar();
product.stop();
person.stop();
cout << "退出程序" << endl;
return 0;
}
超时等待机制
int ret = g_sem.Wait(1000);//设置等待1000ms
if (ret != 0)//如果返回值不为0,表明超时
{
//超时处理
}
|