• windows系统调用 semaphore信号量


     1 #include "iostream"
     2 #include "windows.h"
     3 #include "cstring"
     4 using namespace std;
     5 
     6 HANDLE g_hSemThreads=INVALID_HANDLE_VALUE;
     7 
     8 static DWORD WINAPI ThreadProc(LPVOID lpParam){
     9     LONG nPauseMs=reinterpret_cast<LONG>(lpParam);
    10     Sleep(nPauseMs);
    11 
    12     if(g_hSemThreads!=INVALID_HANDLE_VALUE){
    13         LONG nPrevCt(0);
    14 
    15         if(ReleaseSemaphore(
    16             g_hSemThreads,
    17             1,
    18             &nPrevCt
    19             )){
    20                 printf("%d msec thread ended ,slots left:%d
    ",nPauseMs,nPrevCt+1);
    21         }
    22     }
    23 
    24     return(0);
    25 }
    26 
    27 
    28 
    29 void main(){
    30     g_hSemThreads=CreateSemaphore(
    31         NULL,
    32         5,
    33         5,
    34         NULL
    35         );
    36     for(int nTotal=20;nTotal>0;nTotal-=2){
    37         WaitForSingleObject(g_hSemThreads,INFINITE);
    38         LONG nPauseMs=nTotal*5;
    39 
    40         HANDLE hThread=CreateThread(
    41             NULL,
    42             0,
    43             ThreadProc,
    44             reinterpret_cast<LPVOID>(nPauseMs),
    45             0,
    46             NULL
    47             );
    48 
    49         printf("in the %d msec a thread is creates 
    ",nPauseMs);
    50         CloseHandle(hThread);
    51         hThread=INVALID_HANDLE_VALUE;
    52     }
    53 
    54     CloseHandle(g_hSemThreads);
    55     g_hSemThreads=INVALID_HANDLE_VALUE;
    56     getchar();
    57 }
  • 相关阅读:
    JavaFx在macOS下的文字渲染Bug
    Java多接口同名方法的冲突
    旧技术的惯性
    一点思考(1)
    slisp:编译到JVM平台上的lisp方言
    Arcee:又一个 Parser Generator 轮子
    使用Java实现一门简单的动态语言
    Hello World!
    [NOI2008]奥运物流
    [IOI2005]Riv河流
  • 原文地址:https://www.cnblogs.com/593213556wuyubao/p/3792097.html
Copyright © 2020-2023  润新知