Win32 线程的创建&信号量临界资源&事件对象
2014-04-28 15:54
417 查看
背景:
写了个程序,主要实现的功能为主线程创建两个子线程,之后主线程进入等待事件的循环。子线程1对临界资源cnt的值减2并输出cnt的值。子线程2对cnt的值加1并输出cnt的值,且当cnt为50的时候触发事件对象hend_even,子进程2退出。主线程收到hend_even后杀死子线程1,之后进入scanf阻塞。。。
代码示例:
#include <windows.h>
#include <stdio.h>
DWORD WINAPI funAproc(LPVOID lpParameter);
DWORD WINAPI funBproc(LPVOID lpParameter);
int cnt=100; //共享数据
HANDLE hmutex_cnt; //cnt的信号量锁
HANDLE hend_even; //事件
void main()
{
HANDLE th1,th2;
//false表示当前主线程不拥有该信号,true表示创建该信号后备主线程拥有
hmutex_cnt = CreateMutex(NULL,FALSE,NULL);
//第二个参数:
//true(人工重置事件对象,等待该事件的所有进程都将进入运行
//false(自动重置事件对象),等待该事件中的一个进程可变为调度
//第三个参数:
//true: 创建该事件对象后,触发事件,false:不触发事件。
hend_even = CreateEvent(NULL,TRUE,FALSE,NULL);
int x=2;//带参线程
//funAproc为线程入口,&x为线程函数参数,该参数为(void*)类型,如果多余一个参数可以采用传递结构体的方式
th1 = CreateThread(NULL,0,funAproc,&x,0,NULL);
th2 = CreateThread(NULL,0,funBproc,NULL,0,NULL);
while (TRUE)
{
if (cnt == 0) break;
//Sleep(1);
if(WAIT_OBJECT_0 == WaitForSingleObject(hend_even,100)){//主线程等待th2发出“终止th1的事件”,即hend_even
TerminateThread(th1,1); //终止线程
break;
}
}
printf("back to main thread... \n ");
int i;
while (scanf("%d",&i))
{
;
}
}
DWORD WINAPI funAproc(LPVOID lpParameter){
int b,c;
int k = *(int*)lpParameter;
while (cnt>0)
{
WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
cnt -= k;
printf("thread A delete 2 of cnt = %d\n",cnt);
ReleaseMutex(hmutex_cnt); //释放信号量
}
return 0;
}
DWORD WINAPI funBproc(LPVOID lpParameter){
while (cnt>0)
{
WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
cnt = cnt + 1;
printf("thread B add 1 of cnt = %d\n",cnt);
if (cnt == 50) //当cnt == 50的时候,终止th1和th2,此时th2触发hend_even事件,并退出
{
SetEvent(hend_even);
printf("proc B stop to sail ticket...\n");
ReleaseMutex(hmutex_cnt); //释放信号量
exit(0);
}
ReleaseMutex(hmutex_cnt); //释放信号量
}
return 0;
}
运行结果:
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...
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
写了个程序,主要实现的功能为主线程创建两个子线程,之后主线程进入等待事件的循环。子线程1对临界资源cnt的值减2并输出cnt的值。子线程2对cnt的值加1并输出cnt的值,且当cnt为50的时候触发事件对象hend_even,子进程2退出。主线程收到hend_even后杀死子线程1,之后进入scanf阻塞。。。
代码示例:
#include <windows.h>
#include <stdio.h>
DWORD WINAPI funAproc(LPVOID lpParameter);
DWORD WINAPI funBproc(LPVOID lpParameter);
int cnt=100; //共享数据
HANDLE hmutex_cnt; //cnt的信号量锁
HANDLE hend_even; //事件
void main()
{
HANDLE th1,th2;
//false表示当前主线程不拥有该信号,true表示创建该信号后备主线程拥有
hmutex_cnt = CreateMutex(NULL,FALSE,NULL);
//第二个参数:
//true(人工重置事件对象,等待该事件的所有进程都将进入运行
//false(自动重置事件对象),等待该事件中的一个进程可变为调度
//第三个参数:
//true: 创建该事件对象后,触发事件,false:不触发事件。
hend_even = CreateEvent(NULL,TRUE,FALSE,NULL);
int x=2;//带参线程
//funAproc为线程入口,&x为线程函数参数,该参数为(void*)类型,如果多余一个参数可以采用传递结构体的方式
th1 = CreateThread(NULL,0,funAproc,&x,0,NULL);
th2 = CreateThread(NULL,0,funBproc,NULL,0,NULL);
while (TRUE)
{
if (cnt == 0) break;
//Sleep(1);
if(WAIT_OBJECT_0 == WaitForSingleObject(hend_even,100)){//主线程等待th2发出“终止th1的事件”,即hend_even
TerminateThread(th1,1); //终止线程
break;
}
}
printf("back to main thread... \n ");
int i;
while (scanf("%d",&i))
{
;
}
}
DWORD WINAPI funAproc(LPVOID lpParameter){
int b,c;
int k = *(int*)lpParameter;
while (cnt>0)
{
WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
cnt -= k;
printf("thread A delete 2 of cnt = %d\n",cnt);
ReleaseMutex(hmutex_cnt); //释放信号量
}
return 0;
}
DWORD WINAPI funBproc(LPVOID lpParameter){
while (cnt>0)
{
WaitForSingleObject(hmutex_cnt,INFINITE);//获取信号量,对临界资源(cnt)进行W操作
cnt = cnt + 1;
printf("thread B add 1 of cnt = %d\n",cnt);
if (cnt == 50) //当cnt == 50的时候,终止th1和th2,此时th2触发hend_even事件,并退出
{
SetEvent(hend_even);
printf("proc B stop to sail ticket...\n");
ReleaseMutex(hmutex_cnt); //释放信号量
exit(0);
}
ReleaseMutex(hmutex_cnt); //释放信号量
}
return 0;
}
运行结果:
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...
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
相关文章推荐
- AfxBeginThread创建和事件对象结束线程的方法
- python调用win32com创建可响应应用事件的文档对象
- win32下多线程同步方式之临界区,互斥量,事件对象,信号量
- 用C++实现Win32事件对象,同步线程
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
- 同步事件,信号量,互斥,临界区,线程,线程池C++实现(win32,linux)
- 用c++实现win32事件对象,同步线程
- Jquery基础事件&&事件对象
- 线程间操作无效:从不是创建控件 "xxxx" 的线程访问它。
- gdb动态库延迟断点及线程/进程创建相关事件处理(上)
- 解决weblogic启动慢和创建域慢&线程配置
- Delphi中主窗体的formshow事件在程序自动创建窗体对象前就调用的错误。
- Linux多线程编程之同步对象编程:线程信号量
- 事件对象 互斥对象对 线程的同步
- QObject提供了QMetaObject元类信息(相当于RTTI和反射),信号与连接,父子关系,调试信息,属性,事件,继承关系,窗口类型,线程属性,时间器,对象名称,国际化
- 学习pthreads,使用属性对象创建结合线程和分离线程
- gdb动态库延迟断点及线程/进程创建相关事件处理(
- js创建对象的构造函数模式+原型模式和组合继承\Hybrid Pattern & combination inheritance
- 利用事件对象同步线程
- 对象、类和实例&&线程与进程