国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++中Semaphore內核對象用法實例

2020-01-26 15:15:03
字體:
來源:轉載
供稿:網友

本文實例講述了C++中Semaphore內核對象的用法,分享給大家供大家參考。具體方法如下:

復制代碼 代碼如下:
// Semaphore.cpp : 定義控制臺應用程序的入口點。 
// 
 
#include "stdafx.h" 
#include <Windows.h> 
#include <process.h>  
 
HANDLE g_hSemaphore; 
DWORD g_nConut1 = 0; 
DWORD g_nConut2 = 0; 
unsigned __stdcall ThreadProc1( void* pArguments ) 

    ::WaitForSingleObject(g_hSemaphore, INFINITE); 
    for (int i=0;i<10000;i++) 
    { 
        g_nConut1++; 
        g_nConut2++; 
    } 
    ::ReleaseSemaphore(g_hSemaphore, 1, NULL); 
    printf("ThreadProc1/n"); 
    return 0; 

 
unsigned __stdcall ThreadProc2( void* pArguments ) 

    ::WaitForSingleObject(g_hSemaphore, INFINITE); 
    for (int i=0;i<10000;i++) 
    { 
        g_nConut1++; 
        g_nConut2++; 
    } 
    ::ReleaseSemaphore(g_hSemaphore, 1, NULL); 
    printf("ThreadProc2/n"); 
    return 0; 

 
unsigned __stdcall ThreadProc3( void* pArguments ) 

    ::WaitForSingleObject(g_hSemaphore, INFINITE); 
    for (int i=0;i<10000;i++) 
    { 
        g_nConut1++; 
        g_nConut2++; 
    } 
    ::ReleaseSemaphore(g_hSemaphore, 1, NULL); 
    printf("ThreadProc3/n"); 
    return 0; 

int _tmain(int argc, _TCHAR* argv[]) 

    g_hSemaphore = ::CreateSemaphore(NULL, 2, 2, NULL); 
    HANDLE hThread[3]; 
    hThread[0] = (HANDLE)::_beginthreadex(NULL, 0, ThreadProc1, NULL, 0, NULL); 
    hThread[1] = (HANDLE)::_beginthreadex(NULL, 0, ThreadProc2, NULL, 0, NULL); 
    hThread[2] = (HANDLE)::_beginthreadex(NULL, 0, ThreadProc3, NULL, 0, NULL); 
 
    ::WaitForMultipleObjects(2,hThread,TRUE, INFINITE); 
    printf("g_count1=%d/n", g_nConut1); 
    printf("g_count2=%d/n", g_nConut2); 
    printf("main finished./n"); 
    return 0; 
}

希望本文所述對大家的C++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湛江市| 清镇市| 赫章县| 大方县| 大埔区| 勐海县| 米脂县| 黄冈市| 桦南县| 高碑店市| 织金县| 宿松县| 咸宁市| 稷山县| 安福县| 小金县| 枣阳市| 冀州市| 嫩江县| 永定县| 阜宁县| 台州市| 临高县| 德格县| 略阳县| 潞西市| 大港区| 原阳县| 盐池县| 博湖县| 大英县| 石棉县| 黔南| 郑州市| 榆中县| 无极县| 五寨县| 嵊州市| 顺昌县| 民和| 阳春市|