#include <stdio.h>
#include <windows.h>
#include <iostream.h>
#include <winbase.h>
#define THREAD_INSTANCE_NUMBER 3
//全局变量
int iCounter=0;
DWORD SubThreadA(void * pD)
{
int iID=(int)pD;
//在内部重新打开
HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"sam sp 44");
cout << "Thread " << iID << " begin." << endl;
//设置成为有信号状态
Sleep(1000);
cout << "Thread " << iID << " set event." << endl;
SetEvent(hCounterIn);
Sleep(1000);
cout << "Thread " << iID << " end." << endl;
CloseHandle(hCounterIn);
return 0;
}
DWORD SubThreadB(void* pD)
{//等待threadA结束后在继续执行
int iID=(int)pD;
//在内部重新打开
HANDLE hCounterIn=OpenEvent(EVENT_ALL_ACCESS,FALSE,"sam sp 44");
if(WAIT_TIMEOUT == WaitForSingleObject(hCounterIn,10*1000))
{
cout << "Thread " << iID << " wait time out." << endl;
}
else
{
cout << "Thread " << iID << " wait ok." << endl;
}
CloseHandle(hCounterIn);
return 0;
}
void main(void)
{
HANDLE hCounter=NULL;
if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"sam sp 44"))==NULL)
{
//如果没有其他进程创建这个事件,则重新创建,该事件为人工重置事件
hCounter = CreateEvent(NULL,TRUE/*人工重置*/,FALSE,"sam sp 44");
}
DWORD IDThread[THREAD_INSTANCE_NUMBER];
HANDLE hThread[THREAD_INSTANCE_NUMBER];
int i;
cout << "Test of manual rest event." << endl;
//创建线程
cout << "CreateThread" << endl;
for (i=0;i<THREAD_INSTANCE_NUMBER;i++)
{
if (i==0)
{ // Create one SubThreadA
hThread[i] = CreateThread(NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) SubThreadA, // thread function
(void *)i, // no thread function argument
0, // use default creation flags
&(IDThread[i])); // returns thread identifier
// Check the return value for success.
if (hThread[i] == NULL)
cout << "CreateThread error" << i << endl;
else
cout << "CreateThread: " << i << endl;
}
else
{ // Create two SubThreadB
hThread[i] = CreateThread(NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) SubThreadB, // thread function
(void *)i, // no thread function argument
0, // use default creation flags
&(IDThread[i])); // returns thread identifier
// Check the return value for success.
if (hThread[i] == NULL)
cout << "CreateThread error" << i << endl;
else
cout << "CreateThread: " << i << endl;
}
}
//等待线程结束
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
//关闭句柄
CloseHandle(hCounter);
cout << "Test of auto rest event." << endl;
if( (hCounter=OpenEvent(EVENT_ALL_ACCESS,FALSE,"sam sp 44"))==NULL)
{
//如果没有其他进程创建这个事件,则重新创建,该事件为自动重置事件
hCounter = CreateEvent(NULL,FALSE/*自动重置*/,FALSE,"sam sp 44");
}
//创建线程
cout << "CreateThread" << endl;
for (i=0;i<THREAD_INSTANCE_NUMBER;i++)
{
if (i==0)
{ // Create one SubThreadA
hThread[i] = CreateThread(NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) SubThreadA, // thread function
(void *)i, // no thread function argument
0, // use default creation flags
&(IDThread[i])); // returns thread identifier
// Check the return value for success.
if (hThread[i] == NULL)
cout << "CreateThread error" << i << endl;
else
cout << "CreateThread: " << i << endl;
}
else
{ // Create two SubThreadB
hThread[i] = CreateThread(NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) SubThreadB, // thread function
(void *)i, // no thread function argument
0, // use default creation flags
&(IDThread[i])); // returns thread identifier
// Check the return value for success.
if (hThread[i] == NULL)
cout << "CreateThread error" << i << endl;
else
cout << "CreateThread: " << i << endl;
}
}
//等待线程结束
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER,hThread,TRUE,INFINITE);
//关闭句柄
CloseHandle(hCounter);
cout << "over" << endl;
// Sleep(5000);
return ;
}
os_example.rar_信号量_进程同步
版权申诉
184 浏览量
2022-09-21
03:09:04
上传
评论
收藏 6KB RAR 举报
朱moyimi
- 粉丝: 63
- 资源: 1万+
最新资源
- 课设毕设基于SSM的校园餐厅管理 LW+PPT+源码可运行.zip
- Python井字棋代码
- 课设毕设基于SSM的书店仓库管理系统2021 LW+PPT+源码可运行.zip
- 课设毕设基于SSM的沙县小吃点餐系统 LW+PPT+源码可运行.zip
- 课设毕设基于SSM的旅游景点线路网站 LW+PPT+源码可运行.zip
- EDA实验计数器CNT9999-DTCNT9999实验源代码
- 课设毕设基于SSM的抗疫医疗用品销售平台 LW+PPT+源码可运行.zip
- 基于Halcon的仿照VisonPro的机器视觉软件.zip
- battery-percentage-detector 使用 Javascript 的电池百分比检测器
- 毕业设计基于Qt+FFmpeg+SDL实现的音视频播放器源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈