博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程交叉运行思考
阅读量:6049 次
发布时间:2019-06-20

本文共 3487 字,大约阅读时间需要 11 分钟。

1 // ThreadPrintOddEven.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。  2 //  3   4 #include "pch.h"  5 #include 
6 #include
7 #include
8 #include
9 #include
10 11 12 13 14 int g_number = 0; 15 HANDLE g_hEvent = NULL; 16 BOOL g_isFirst = true; 17 18 unsigned int __stdcall threadFun1(void *param) 19 { 20 printf("Enter %s\n", __FUNCTION__); 21 22 while (1) 23 { 24 if (g_isFirst) 25 { 26 SetEvent(g_hEvent); 27 g_isFirst = false; 28 } 29 DWORD dwWaitResult = WaitForSingleObject(g_hEvent, INFINITE); 30 31 switch (dwWaitResult) 32 { 33 // Event object was signaled 34 case WAIT_OBJECT_0: 35 break; 36 // An error occurred 37 default: 38 printf("Wait error (%d)\n", GetLastError()); 39 return 0; 40 } 41 42 ResetEvent(g_hEvent); 43 44 printf("threadFun1: AAAAAAAAAAAAAAAAAAAA\t\r\n"); 45 Sleep(850); 46 47 SetEvent(g_hEvent); 48 } 49 50 return 0; 51 } 52 53 unsigned int __stdcall threadFun2(void *param) 54 { 55 printf("Enter %s\n", __FUNCTION__); 56 57 while (1) 58 { 59 DWORD dwWaitResult = WaitForSingleObject(g_hEvent, INFINITE); 60 61 switch (dwWaitResult) 62 { 63 // Event object was signaled 64 case WAIT_OBJECT_0: 65 break; 66 // An error occurred 67 default: 68 printf("Wait error (%d)\n", GetLastError()); 69 return 0; 70 } 71 72 ResetEvent(g_hEvent); 73 74 printf("threadFun2: BBBBBBBBBBBBBBBBBBBB\t\r\n"); 75 Sleep(400); 76 77 SetEvent(g_hEvent); 78 //Sleep(800); 79 80 } 81 82 return 0; 83 } 84 85 unsigned int __stdcall threadFun3(void *param) 86 { 87 printf("Enter %s\n", __FUNCTION__); 88 89 while (1) 90 { 91 DWORD dwWaitResult = WaitForSingleObject(g_hEvent, INFINITE); 92 93 switch (dwWaitResult) 94 { 95 // Event object was signaled 96 case WAIT_OBJECT_0: 97 break; 98 // An error occurred 99 default:100 printf("Wait error (%d)\n", GetLastError());101 return 0;102 }103 104 ResetEvent(g_hEvent); 105 printf("threadFun3: CCCCCCCCCCCCCCCCCCCC\t\r\n");106 Sleep(600);107 108 SetEvent(g_hEvent);109 //Sleep(800);110 }111 112 return 0;113 }114 115 int main(int argc, char* argv[])116 {117 g_hEvent = CreateEvent(NULL, false, 0, L"Event Test1");118 119 HANDLE hThread1 = NULL;120 unsigned threadID1 = 0;121 122 hThread1 = (HANDLE)_beginthreadex(NULL, 0, threadFun1, NULL, 0, &threadID1);123 124 HANDLE hThread2 = NULL;125 unsigned threadID2 = 0;126 127 hThread2 = (HANDLE)_beginthreadex(NULL, 0, threadFun2, NULL, 0, &threadID2);128 129 HANDLE hThread3 = NULL;130 unsigned threadID3 = 0;131 132 hThread3 = (HANDLE)_beginthreadex(NULL, 0, threadFun3, NULL, 0, &threadID3);133 134 SetThreadPriority(hThread1, THREAD_PRIORITY_ABOVE_NORMAL);135 SetThreadPriority(hThread2, THREAD_PRIORITY_BELOW_NORMAL);136 137 while (1);138 139 return 0;140 }

 

转载于:https://www.cnblogs.com/endenvor/p/10523542.html

你可能感兴趣的文章
LAMP编译部署
查看>>
XenDesktop7.6安装部署入门教程
查看>>
HashMap的工作原理及HashMap和Hashtable的区别
查看>>
GregorianCalendar日历程序
查看>>
Sublime 中运行 Shell 、Python、Lua、Groovy...等各种脚本
查看>>
【Java集合源码剖析】ArrayList源码剖析
查看>>
linux的目录结构
查看>>
这次逻辑通了,
查看>>
HTMLHelper
查看>>
快速构建Windows 8风格应用29-捕获图片与视频
查看>>
OC语言Block和协议
查看>>
使用xpath时出现noDefClass的错误(找不到某个类)
查看>>
.Net规则引擎介绍 - REngine
查看>>
CSS3 transforms 3D翻开
查看>>
利用传入的Type类型来调用范型方法的解决方案
查看>>
Top命令内存占用剖析
查看>>
转 网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO
查看>>
求带分数(蓝桥杯)
查看>>
Retrofit 入门学习
查看>>
Spring Boot学习笔记
查看>>