WaitForSingleObject&WaitForMultipleObjects
来源:互联网 发布:粤嵌培训怎么样知乎 编辑:程序博客网 时间:2024/05/16 08:48
WaitForSingleObject
#include "stdafx.h"
#include <iostream>#include <windows.h>
using namespace std;
DWORD WINAPI ThreadFun(PVOID par)
{
cout<<"线程方法正在执行..."<<endl;
Sleep(3000);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD ThreadID;
HANDLE hThread=CreateThread(NULL,0,ThreadFun,(void*)0,0,NULL);
while(TRUE)
{
//这里等待时间是设置的1秒钟,可以设置为INFINITE(就不需要外面的死循环了)
DWORD r= WaitForSingleObject(hThread,1000);
if(r==WAIT_TIMEOUT)
{
cout<<"WAIT_TIMEOUT"<<endl;//输出2-3次
continue;
}
else if(r==WAIT_OBJECT_0)
{
cout<<"WAIT_OBJECT_0"<<endl;//线程方法睡眠3秒以后输出
break;
}
else
{
cout<<"hello"<<endl;
continue;
}
}
CloseHandle(hThread);
system("PAUSE");
return 0;
}
WaitForMultipleObjects
#include "stdafx.h"
#include <iostream>
#include <process.h>
#include <windows.h>
#define ThMax 3//线程数
using namespace std;
unsigned __stdcall ThreadFun(void* par)
{
cout<<"线程"<<*((int*)par)<<"正在执行..."<<endl;
Sleep(3000);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThreads[ThMax];
int par[]={1,2,3};
int i=0;
while(TRUE)
{
if(i<3)
{
hThreads[i]=(HANDLE)_beginthreadex(NULL,0,&ThreadFun,(void*)&(par[i]),0,NULL);
Sleep(500);
}
else
{
//详细参考:http://msdn.microsoft.com/en-us/library/ms687025(v=vs.85).aspx
//bWaitAll参数如果设置为TRUE,那么就必须所有核心对象是激发状态才会返回,FALSE则一个就返回
DWORD r=WaitForMultipleObjects(3,hThreads,TRUE,1000);
if(r==WAIT_TIMEOUT)
{
cout<<"WAIT_TIMEOUT"<<endl;
continue;
}
else if(r==WAIT_OBJECT_0)
{
cout<<"WAIT_OBJECT_0"<<endl;
break;
}
else
{
cout<<"等待"<<endl;
continue;
}
}
i++;
}
for(int i=0;i<ThMax;i++)
{
CloseHandle(hThreads[i]);
}
cout<<"执行完毕"<<endl;
system("PAUSE");
return 0;
}
- WaitForSingleObject & WaitForMultipleObjects
- WaitForSingleObject&WaitForMultipleObjects
- Sleep和WaitForSingleObject ,WaitForMultipleObjects
- Sleep和WaitForSingleObject ,WaitForMultipleObjects
- WaitForSingleObject和WaitForMultipleObjects用法
- WaitForSingleObject和 WaitForMultipleObjects
- WaitForSingleObject WaitForMultipleObjects 用法
- WaitForSingleObject、WaitForMultipleObjects、CreateThread
- WaitForSingleObject和WaitForMultipleObjects用法
- WaitForSingleObject 和 WaitForMultipleObjects
- WaitForSingleObject和WaitForMultipleObjects
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject、WaitForMultipleObjects、CreateThread .
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject与waitForMultipleObjects
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject以及WaitForMultipleObjects 函数
- Solaris+Oracle启动实例及切换多实例
- js jquery父页面 子页面互相调用
- Redis 的 5 个常见使用场景
- tcp长连接和保活时间
- MBED移植日记:exti_api(基于STM32F107RC,适用于C)
- WaitForSingleObject&WaitForMultipleObjects
- Xcode8去掉控制台无用打印信息
- 左边列表移到到右边列表
- leetcode 85. Maximal Rectangle
- 小端模式和大端模式
- RecyclerView详解 —— 自定义动画
- 二查平衡树的插入与删除(四种旋转)
- 15.2、SQLiteOpenHelper
- 《JAVA与模式》之调停者模式