016 内核对象的Signal状态3
Posted ☆﹎夜﹎☆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了016 内核对象的Signal状态3相关的知识,希望对你有一定的参考价值。
Signal
● Signal = FALSE 相当于标志
● 进程 初始化FALSE 运行完成 设置位 TURE
● 线程
● 标准输入输出流
● 互斥体
● 信号
● 可等待计时器
● 作业
● WaitForSingleObject() 句柄,时间
○ 当前正在执行的线程,变成不可调度的状态,
○ 等待Signal 状态变成 True
○ 返回值 WAIT_TIMEOUT 超时
○ 返回值 WAIT_FAILED 方法失败 可以使用 GetLastError获取错误
○ 返回值 WAIT_OBJECT_0 指定的对象出有有信号状态
○ 当hHandle为mutex时,如果拥有mutex的线程在结束时没有释放核心对象会引发此返回值。
● WaitForMultipleObjects()等待多个信号状态
1.等待几个
2数组
3是否等待所有
4等待时间
○ 返回值WAIT_OBJECT_0 (WAIT_OBJECT_0 + nCount - 1如果bWaitAll为TRUE),则返回值表明所有指定对象的状态信号。
○ 返回值WAIT_ABANDONED_0 (WAIT_ABANDONED_0 + nCount - 1)如果bWaitAll为TRUE,则返回值表明所有指定对象的状态是触发的,并且至少对象之一,是一个废弃的互斥对象。
○ 返回值WAIT_TIMEOUTT超时间隔已过
● 同步
○ 进程和进程之间的同步
○ 守护进程 父进程
1 #define UNICODE 2 #include <stdio.h> 3 #include <Windows.h> 4 #include <process.h> 5 6 unsigned __stdcall ThreadFunc(void *lParam) 7 { 8 return 0; 9 } 10 11 int main() 12 { 13 HANDLE hThread = (HANDLE)_beginthreadex(nullptr, 0, ThreadFunc, nullptr, 0, nullptr); 14 //WaitForSingleObject(hThread, INFINITE); //永远等待 ->30天 15 DWORD dw = WaitForSingleObject(hThread, 1000); 16 17 switch(dw) 18 { 19 case WAIT_TIMEOUT: 20 printf("等待超时!\\r\\n"); 21 break; 22 case WAIT_OBJECT_0: 23 printf("成功等待!\\r\\n"); 24 break; 25 case WAIT_FAILED: 26 break; 27 } 28 }
WaitForMultipleObjects
1 #define UNICODE 2 #include <stdio.h> 3 #include <Windows.h> 4 #include <process.h> 5 6 unsigned __stdcall ThreadFunc(void *lParam) 7 { 8 Sleep(2000); 9 return 0; 10 } 11 12 int main() 13 { 14 HANDLE hThread = (HANDLE)_beginthreadex(nullptr, 0, ThreadFunc, nullptr, 0, nullptr); 15 //WaitForSingleObject(hThread, INFINITE); //永远等待 ->30天 16 DWORD dw = WaitForSingleObject(hThread, 1000); 17 18 switch(dw) 19 { 20 case WAIT_TIMEOUT: 21 printf("等待超时!\\r\\n"); 22 break; 23 case WAIT_OBJECT_0: 24 printf("成功等待!\\r\\n"); 25 break; 26 case WAIT_FAILED: 27 break; 28 } 29 }
1 #define UNICODE 2 #include <stdio.h> 3 #include <Windows.h> 4 #include <process.h> 5 6 unsigned __stdcall ThreadFunc(void *lParam) 7 { 8 Sleep((int)lParam); 9 return 0; 10 } 11 12 int main() 13 { 14 HANDLE hThread[2] = {INVALID_HANDLE_VALUE}; 15 hThread[0] = (HANDLE)_beginthreadex(nullptr, 0, ThreadFunc, (void*)100, 0, nullptr); 16 hThread[1] = (HANDLE)_beginthreadex(nullptr, 0, ThreadFunc, (void*)500, 0, nullptr); 17 //WaitForSingleObject(hThread, INFINITE); //永远等待 ->30天 18 //DWORD dw = WaitForSingleObject(hThread, 1000); 19 //DWORD dw = WaitForMultipleObjects(2,hThread,FALSE,2000); 20 BOOL bLoop = TRUE; 21 DWORD dwRet = -1; 22 while (bLoop) 23 { 24 dwRet = WaitForMultipleObjects(2,hThread,FALSE,100); 25 DWORD dw = WaitForSingleObject(hThread[1],1); 26 if(dw == WAIT_OBJECT_0) 27 { 28 printf("Thread 2 is Reading.........\\r\\n"); 29 } 30 switch(dwRet) 31 { 32 case WAIT_TIMEOUT: 33 printf("Wait Time Out , Next Check....\\r\\n"); 34 break; 35 case WAIT_OBJECT_0: 36 printf("Thread 1 is OK..........\\r\\n"); 37 break; 38 case WAIT_OBJECT_0 +1: 39 printf("Thread 2 is OK........\\r\\n"); 40 bLoop = FALSE; 41 break; 42 case WAIT_FAILED: 43 printf("Error.........\\r\\n"); 44 break; 45 } 46 } 47 return 0; 48 /* 49 switch(dw) 50 { 51 case WAIT_TIMEOUT: 52 printf("等待超时!\\r\\n"); 53 break; 54 case WAIT_OBJECT_0: 55 printf("成功等待!\\r\\n"); 56 break; 57 case WAIT_FAILED: 58 break; 59 } 60 */ 61 }
以上是关于016 内核对象的Signal状态3的主要内容,如果未能解决你的问题,请参考以下文章