C++ 线程同步---信号量
·
最近一直在学习OBS源码,遇到很多多线程的知识,也算是恶补了一下之前没搞明白一直没机会用的信号量。
一、相关的函数和头文件介绍
//头文件
#include <windows.h>
//创建信号量API
HANDLE WINAPI CreateSemaphore(
_In_opt_ LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,//指向SECURITY_ATTRIBUTES的指针;
_In_ LONG lInitialCount, //信号量对象的初始值;
_In_ LONG lMaximumCount, //信号量对象的最大值,这个值必须大于0;
_In_opt_ LPCTSTR lpName //信号量对象的名称;
);
//等待信号量API
DWORD WINAPI WaitForSingleObject(
_In_ HANDLE hHandle, //信号量对象句柄
_In_ DWORD dwMilliseconds //等待信号量时间,INFINET代表永久等待;
);
返回值:
WAIT_ABANDONED(0x00000080L) 表示拥有信号量的线程再终止前未释放该信号量;
WAIT_OBJECT_0(0x00000000L) 表示等到了信号量;
WAIT_TIMEOUT(0x00000102L) 表示等待超时;
WAIT_FAILED((DWORD)0xFFFFFFFF) 表示该函数执行失败,用GetLastError()得到错误码;
//释放信号量句柄
BOOL WINAPI ReleaseSemaphore(
_In_ HANDLE hSemaphore, //信号量对象句柄;
_In_ LONG lReleaseCount, //信号量释放的值,必须大于0;
_Out_opt_ LPLONG lpPreviousCount //前一次信号量值的指针,不需要可置为空;
);
返回值:成功返回非0;
二、win32平台下的demo
//#include "stdafx.h"
#include<windows.h>
#include<iostream>
using namespace std;
int number = 1; //定义全局变量
HANDLE hSemaphore; //定义信号量句柄
unsigned long __stdcall ThreadProc1(void* lp)
{
long count;
while (number < 100)
{
WaitForSingleObject(hSemaphore, INFINITE); //等待信号量为有信号状态
cout << "thread 1 :" << number << endl;
++number;
Sleep(100);
ReleaseSemaphore(hSemaphore, 1, &count);
}
return 0;
}
unsigned long __stdcall ThreadProc2(void* lp)
{
long count;
while (number < 100)
{
WaitForSingleObject(hSemaphore, INFINITE); //等待信号量为有信号状态
cout << "thread 2 :" << number << endl;
++number;
Sleep(100);
ReleaseSemaphore(hSemaphore, 1, &count);
}
return 0;
}
int main()
{
LPCWSTR lpName = L"mm";
hSemaphore = CreateSemaphore(NULL, 1, 100, lpName);
CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL);
Sleep(10 * 1000);
system("pause");
return 0;
}
三、运行结果
thread 1 :1
thread 2 :2
thread 1 :3
thread 2 :4
thread 1 :5
thread 2 :6
thread 1 :7
thread 2 :8
thread 1 :9
thread 2 :10
thread 1 :11
thread 2 :12
thread 1 :13
thread 2 :14
thread 1 :15
thread 2 :16
thread 1 :17
thread 2 :18
thread 1 :19
thread 2 :20
thread 1 :21
thread 2 :22
thread 1 :23
thread 2 :24
thread 1 :25
thread 2 :26
thread 1 :27
thread 2 :28
thread 1 :29
thread 2 :30
thread 1 :31
thread 2 :32
thread 1 :33
thread 2 :34
thread 1 :35
thread 2 :36
thread 1 :37
thread 2 :38
thread 1 :39
thread 2 :40
thread 1 :41
thread 2 :42
thread 1 :43
thread 2 :44
thread 1 :45
thread 2 :46
thread 1 :47
thread 2 :48
thread 1 :49
thread 2 :50
thread 1 :51
thread 2 :52
thread 1 :53
thread 2 :54
thread 1 :55
thread 2 :56
thread 1 :57
thread 2 :58
thread 1 :59
thread 2 :60
thread 1 :61
thread 2 :62
thread 1 :63
thread 2 :64
thread 1 :65
thread 2 :66
thread 1 :67
thread 2 :68
thread 1 :69
thread 2 :70
thread 1 :71
thread 2 :72
thread 1 :73
thread 2 :74
thread 1 :75
thread 2 :76
thread 1 :77
thread 2 :78
thread 1 :79
thread 2 :80
thread 1 :81
thread 2 :82
thread 1 :83
thread 2 :84
thread 1 :85
thread 2 :86
thread 1 :87
thread 2 :88
thread 1 :89
thread 2 :90
thread 1 :91
thread 2 :92
thread 1 :93
thread 2 :9请按任意键继续. . . 4
thread 1 :95
thread 2 :96
thread 1 :97
thread 2 :98
thread 1 :99
thread 2 :100
更多推荐



所有评论(0)