天天看点

Win32 线程的创建&信号量临界资源&事件对象

背景:

写了个程序,主要实现的功能为主线程创建两个子线程,之后主线程进入等待事件的循环。子线程1对临界资源cnt的值减2并输出cnt的值。子线程2对cnt的值加1并输出cnt的值,且当cnt为50的时候触发事件对象hend_even,子进程2退出。主线程收到hend_even后杀死子线程1,之后进入scanf阻塞。。。

代码示例:

运行结果:

thread a delete 2 of cnt = 98

thread b add 1 of cnt = 99

thread a delete 2 of cnt = 97

thread b add 1 of cnt = 98

thread a delete 2 of cnt = 96

thread b add 1 of cnt = 97

thread a delete 2 of cnt = 95

thread b add 1 of cnt = 96

thread a delete 2 of cnt = 94

thread b add 1 of cnt = 95

thread a delete 2 of cnt = 93

thread b add 1 of cnt = 94

thread a delete 2 of cnt = 92

thread b add 1 of cnt = 93

thread a delete 2 of cnt = 91

thread b add 1 of cnt = 92

thread a delete 2 of cnt = 90

thread b add 1 of cnt = 91

thread a delete 2 of cnt = 89

thread b add 1 of cnt = 90

thread a delete 2 of cnt = 88

thread b add 1 of cnt = 89

...

thread a delete 2 of cnt = 51

thread b add 1 of cnt = 52

thread a delete 2 of cnt = 50

thread b add 1 of cnt = 51

thread a delete 2 of cnt = 49

thread b add 1 of cnt = 50

proc b stop to sail ticket...

back to main thread...

参考:

http://www.cnblogs.com/1314nnnkkk/p/3386375.html

http://hi.baidu.com/kdlipm/item/005be8eb29d785e1fa42bae5

继续阅读