#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <process.h>
#include <locale.h>
TCHAR buf[256];
HANDLE hProduceEvent, hConsumerEvent;
unsigned int __stdcall Consumer(void* p)
{
while (1)
{
WaitForSingleObject(hConsumerEvent,INFINITE);
_tprintf(_T("生产者说:%s\n"),buf);
SetEvent(hProduceEvent);//通知生产者
}
return 0;
}
int main()
{
//使用事件内核对象实现生产者消费者问题
setlocale(LC_ALL,"chs");
/*CreateEvent的第二个参数为TRUE时表示必须手动调用ResetEvent函数使其变为无信号,为FALSE时,
WaitForSingleObject函数返回后,对象自动就被置为无信号。*/
hProduceEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
hConsumerEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
HANDLE hThread = (HANDLE)_beginthreadex(nullptr, 0, Consumer, nullptr, 0, nullptr);
while (1)
{
WaitForSingleObject(hProduceEvent, INFINITE);
_tscanf(_T("%s"),buf);
SetEvent(hConsumerEvent);//通知消费者
}
getchar();
return 0;
}
Windows使用事件内核对象实现生产者消费者问题
最新推荐文章于 2022-06-06 17:35:35 发布