スレッド同期の信号量(「旗」は方向、「旗」はイメージ、「旗」は指揮棒)


チケット販売システムか、直接コードをつけるか、スレッドが同期していない問題を解決します.
#include 
#include 

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 
#include 

#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 thread 2 sell ticket:9 thread 3 sell ticket:8 thread 4 sell ticket:7 thread 5 sell ticket:6 thread 1 sell ticket:5 thread 2 sell ticket:4 thread 3 sell ticket:3 thread 4 sell ticket:2 thread 5 sell ticket:1最後にflagが印刷されなかった理由を分析します.elseに対応する文ブロックにはReleaseSemaphoreがないからです.