线程同步之信号量(“旗帜“就是方向,“旗帜“就是形象, “旗帜“就是指挥棒)

972 篇文章 329 订阅
32 篇文章 6 订阅

       还是售票系统,还是直接上代码,还是解决线程不同步的问题:

#include <windows.h>
#include <iostream.h>

int tickets = 100;
HANDLE g_hSemp = NULL;

DWORD WINAPI ThreadProc1(LPVOID pVoid);
DWORD WINAPI ThreadProc2(LPVOID pVoid);

int main()
{
	// 创建信号量(内核对象),并将活跃的"旗帜数"设置为1, 最大的可用"旗帜数"为10
	g_hSemp = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread[2] = {0};
	hThread[0] = CreateThread(NULL, 0, ThreadProc1, NULL, 0, NULL);
	hThread[1] = CreateThread(NULL, 0, ThreadProc2, NULL, 0, NULL);

	// 等待...
	WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
	
	// 该语句没有机会执行(思考一下为什么)
	cout << "flag" << endl;
	
	CloseHandle(hThread[0]);
	CloseHandle(hThread[1]);
	CloseHandle(g_hSemp);

	return 0;
}

DWORD WINAPI ThreadProc1(LPVOID pVoid)
{
	while(1)
	{
		// 等待,使线程1获得"旗帜"
		WaitForSingleObject(g_hSemp, INFINITE);

		if(tickets > 0)
		{
			Sleep(1);
			cout << "thread1 sell ticket : " << tickets-- << endl;
		}
		else
		{
			break;
		}

		// 释放"旗帜",放弃也是一种智慧,否则上面的WaitForSingleObject会死等
		ReleaseSemaphore(g_hSemp, 1, NULL);
	}

	return 0;
}

DWORD WINAPI ThreadProc2(LPVOID pVoid)
{
	while(1)
	{
		// 等待,使线程2获得"旗帜"
		WaitForSingleObject(g_hSemp, INFINITE);

		if(tickets > 0)
		{
			Sleep(1);
			cout << "thread2 sell ticket : " << tickets-- << endl;
		}
		else
		{
			break;
		}

		// 释放"旗帜",放弃也是一种智慧,否则上面的WaitForSingleObject会死等
		ReleaseSemaphore(g_hSemp, 1, NULL);
	}

	return 0;
}

       这个程序非常重要,弄懂它对理解多线程同步很有好处。有兴趣的读者可以尝试改动程序,然后想想会产生什么结果,并与改动后运行的结果进行对比,好好分析一下。

       下面,我们欣赏一个类似的程序:

#include <windows.h>
#include <iostream.h>

#define THREAD_NUM 5

int tickets = 100;
HANDLE g_hSemp = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	// 创建信号量对象(内核对象),并将活跃的"旗帜数"设置为1, 最大的可用"旗帜数"为10
	g_hSemp = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread[THREAD_NUM] = {0};

	int i;
	for(i = 0; i < THREAD_NUM; i++)
	{
		hThread[i] = CreateThread(NULL, 0, ThreadProc, &i, 0, NULL);
	}

	// 等待...
	WaitForMultipleObjects(THREAD_NUM, hThread, TRUE, INFINITE);
	
	// 该语句没有机会执行(思考一下为什么)
	cout << "flag" << endl;
	
	for(i = 0; i < THREAD_NUM; i++ )
	{
        CloseHandle(hThread[i]);
	}

	CloseHandle(g_hSemp);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	int n = *(int *)pVoid;	
	while(1)
	{
		// 等待,使线程获得"旗帜"
		WaitForSingleObject(g_hSemp, INFINITE);

		if(tickets > 0)
		{
			Sleep(100);
			cout << "thread" << n << " sell ticket:" << tickets-- << endl;
		}
		else
		{
			break;
		}

		// 释放"旗帜",放弃也是一种智慧,否则上面的WaitForSingleObject会死等
		ReleaseSemaphore(g_hSemp, 1, NULL);
	}

	return 0;
}

      结果为:

thread1 sell ticket:100
thread2 sell ticket:99
thread3 sell ticket:98
thread4 sell ticket:97
thread5 sell ticket:96
thread1 sell ticket:95
thread2 sell ticket:94
thread3 sell ticket:93
thread4 sell ticket:92
thread5 sell ticket:91
thread1 sell ticket:90
thread2 sell ticket:89
thread3 sell ticket:88
thread4 sell ticket:87
thread5 sell ticket:86
thread1 sell ticket:85
thread2 sell ticket:84
thread3 sell ticket:83
thread4 sell ticket:82
thread5 sell ticket:81
thread1 sell ticket:80
thread2 sell ticket:79
thread3 sell ticket:78
thread4 sell ticket:77
thread5 sell ticket:76
thread1 sell ticket:75
thread2 sell ticket:74
thread3 sell ticket:73
thread4 sell ticket:72
thread5 sell ticket:71
thread1 sell ticket:70
thread2 sell ticket:69
thread3 sell ticket:68
thread4 sell ticket:67
thread5 sell ticket:66
thread1 sell ticket:65
thread2 sell ticket:64
thread3 sell ticket:63
thread4 sell ticket:62
thread5 sell ticket:61
thread1 sell ticket:60
thread2 sell ticket:59
thread3 sell ticket:58
thread4 sell ticket:57
thread5 sell ticket:56
thread1 sell ticket:55
thread2 sell ticket:54
thread3 sell ticket:53
thread4 sell ticket:52
thread5 sell ticket:51
thread1 sell ticket:50
thread2 sell ticket:49
thread3 sell ticket:48
thread4 sell ticket:47
thread5 sell ticket:46
thread1 sell ticket:45
thread2 sell ticket:44
thread3 sell ticket:43
thread4 sell ticket:42
thread5 sell ticket:41
thread1 sell ticket:40
thread2 sell ticket:39
thread3 sell ticket:38
thread4 sell ticket:37
thread5 sell ticket:36
thread1 sell ticket:35
thread2 sell ticket:34
thread3 sell ticket:33
thread4 sell ticket:32
thread5 sell ticket:31
thread1 sell ticket:30
thread2 sell ticket:29
thread3 sell ticket:28
thread4 sell ticket:27
thread5 sell ticket:26
thread1 sell ticket:25
thread2 sell ticket:24
thread3 sell ticket:23
thread4 sell ticket:22
thread5 sell ticket:21
thread1 sell ticket:20
thread2 sell ticket:19
thread3 sell ticket:18
thread4 sell ticket:17
thread5 sell ticket:16
thread1 sell ticket:15
thread2 sell ticket:14
thread3 sell ticket:13
thread4 sell ticket:12
thread5 sell ticket:11
thread1 sell ticket:10
thread2 sell ticket:9
thread3 sell ticket:8
thread4 sell ticket:7
thread5 sell ticket:6
thread1 sell ticket:5
thread2 sell ticket:4
thread3 sell ticket:3
thread4 sell ticket:2
thread5 sell ticket:1

     最后,分析一下为什么没有打印出flag?因为else对应的语句块没有ReleaseSemaphore.

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值