• 多线程交叉运行思考


      1 // ThreadPrintOddEven.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
      2 //
      3 
      4 #include "pch.h"
      5 #include <iostream>
      6 #include <Windows.h>
      7 #include <time.h>
      8 #include <stdlib.h>
      9 #include <process.h>
     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
    ", __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)
    ", GetLastError());
     39             return 0;
     40         }
     41 
     42         ResetEvent(g_hEvent);
     43 
     44         printf("threadFun1: AAAAAAAAAAAAAAAAAAAA	
    ");
     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
    ", __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)
    ", GetLastError());
     69             return 0;
     70         }
     71 
     72         ResetEvent(g_hEvent);
     73                            
     74         printf("threadFun2: BBBBBBBBBBBBBBBBBBBB	
    ");
     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
    ", __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)
    ", GetLastError());
    101             return 0;
    102         }
    103 
    104         ResetEvent(g_hEvent);                        
    105         printf("threadFun3: CCCCCCCCCCCCCCCCCCCC	
    ");
    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 }
  • 相关阅读:
    小学生都能学会的python(文件操作)
    小学生都能学会的python(深浅拷贝)
    小学生都能学会的python(小数据池)
    小学生都能学会的python(字典{ })
    小学生都能学会的python(列表[ ])
    小学生都能学会的python(编码 and 字符串)
    小学生都能学会的python(一)2018.9.3
    Ionic常用命令
    Ionic1.x项目中的Installing npm packages问题
    hdu1005
  • 原文地址:https://www.cnblogs.com/endenvor/p/10523542.html
Copyright © 2020-2023  润新知