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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 墨竹工卡县| 株洲县| 林甸县| 迭部县| 大邑县| 临泉县| 海安县| 霍州市| 石城县| 清涧县| 永德县| 延庆县| 长治市| 九龙城区| 濉溪县| 信丰县| 曲周县| 兴仁县| 商城县| 若尔盖县| 长沙县| 寻甸| 五常市| 大冶市| 班戈县| 景泰县| 化德县| 新丰县| 钟祥市| 温州市| 莒南县| 文昌市| 莱西市| 九江市| 南昌县| 明光市| 青冈县| 西平县| 淮南市| 建平县| 万安县|