事件对象_打印字符串_2线程_同步

来源:互联网 发布:几个程序员去吃饭 编辑:程序博客网 时间:2024/06/12 21:46
#include <windows.h>//包含头文件#include <stdio.h>DWORD WINAPI myfun1(LPVOID lpParameter);//声明线程函数DWORD WINAPI myfun2(LPVOID lpParameter);HANDLE hevent;//定义全局变量heventint a=0;//定义全局变量aint main(){HANDLE h1,h2;//定义线程句柄hevent=::CreateEvent(NULL,false,false,NULL);::SetEvent(hevent);h1=::CreateThread(NULL,0,myfun1,NULL,0,NULL);//创建线程1printf("线程1开始运行!\r\n");h2=::CreateThread(NULL,0,myfun2,NULL,0,NULL);//创建线程2printf("线程2开始运行!\r\n");::CloseHandle(h1);//关闭线程句柄对象::CloseHandle(h2);::Sleep(100000);//程序睡眠10秒return 0;}DWORD WINAPI myfun1(LPVOID lpParameter) //线程函数1{while(1){::WaitForSingleObject(hevent,INFINITE);//请求事件对象if(a<10000){a+=1;//变量自加::Sleep(1000);//线程睡眠1秒printf("线程1:%d\r\n",a);//输出变量::SetEvent(hevent);//设置事件对象为有信号状态}else{::SetEvent(hevent);//设置事件对象为有信号状态break;//跳出循环}}    return 0;}DWORD WINAPI myfun2(LPVOID lpParameter) //线程函数2{while(1){::WaitForSingleObject(hevent,INFINITE);//请求事件对象if(a<10000){a+=1;::Sleep(1000);printf("线程2:%d\r\n",a);//输出变量::SetEvent(hevent);}else{::SetEvent(hevent);//设置事件对象为有信号状态break;//跳出循环}}    return 0;//线程正常退出}

原创粉丝点击