gpt4 book ai didi

C++中Semaphore内核对象用法实例

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 33 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C++中Semaphore内核对象用法实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了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++程序设计有所帮助.

最后此篇关于C++中Semaphore内核对象用法实例的文章就讲到这里了,如果你想了解更多关于C++中Semaphore内核对象用法实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com