C++中Semaphore内核对象用法实例
2019/7/10 23:11:30
本文主要是介绍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;
}
//
#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内核对象用法实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding