国产探花免费观看_亚洲丰满少妇自慰呻吟_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++程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克陶县| 麻城市| 柞水县| 香港| 苍南县| 岳西县| 大厂| 慈溪市| 许昌县| 达孜县| 盘锦市| 泸溪县| 台州市| 城口县| 兴义市| 安仁县| 樟树市| 和顺县| 通海县| 福清市| 崇文区| 灌南县| 乐至县| 扎兰屯市| 华阴市| 芜湖县| 高州市| 安泽县| 墨玉县| 彭山县| 临洮县| 定州市| 鄱阳县| 克什克腾旗| 新乡市| 浙江省| 林口县| 丹江口市| 哈尔滨市| 揭阳市| 和平县|