首页 > 编程语言 > 详细

vc++信号量同步机制例子

时间:2015-04-17 22:17:11      阅读:404      评论:0      收藏:0      [点我收藏+]
HANDLE hSemaphore;
cout<<1<<endl;

hSemaphore = CreateSemaphore( NULL, 0, 10000, NULL); 

ReleaseSemaphore(hSemaphore, 1, NULL);
ReleaseSemaphore(hSemaphore, 1, NULL);

DWORD dwWaitResult = WaitForSingleObject(hSemaphore, INFINITE);
	cout<<2<<endl;
 WaitForSingleObject(hSemaphore, INFINITE);
	cout<<3<<endl;

WaitForSingleObject(hSemaphore, INFINITE);
	cout<<4<<endl;

输出

1

2

3

_

//CreateSemaphore( NULL, 0, 100, NULL); 初始信号为0

//ReleaseSemaphore(hSemaphore, 1, NULL);信号加1

//WaitForSingleObject(hSemaphore, INFINITE);阻塞式等待,返回时,信号减1


线程间的通知:

如果线程B需要线程A的数据。

当线程A的数据增加时,可以ReleaseSemaphore(hSemaphore, 1, NULL);

线程B使用WaitForSingleObject(hSemaphore, INFINITE);来判断线程A有无数据

vc++信号量同步机制例子

原文:http://blog.csdn.net/h1023417614/article/details/45101979

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!