几个简单程序加深你对多线程和信号量的理解

972 篇文章 329 订阅
32 篇文章 6 订阅
#include <windows.h>
#include <iostream.h>

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);

	return 0;
}

     结果:

main thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);

	return 0;
}

     结果:

main thread

thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 0, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);

	return 0;
}

     结果:

main thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	WaitForSingleObject(g_hSemp2, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);
	ReleaseSemaphore(g_hSemp2, 1, NULL);

	return 0;
}

     结果:

main thread

thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 0, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	WaitForSingleObject(g_hSemp2, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);
	ReleaseSemaphore(g_hSemp2, 1, NULL);

	return 0;
}

     结果:

main thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 1, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	WaitForSingleObject(g_hSemp1, INFINITE);
	cout << "thread" << endl;
	ReleaseSemaphore(g_hSemp1, 1, NULL);
	ReleaseSemaphore(g_hSemp2, 1, NULL);

	return 0;
}

     结果:

main thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 0, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	WaitForSingleObject(g_hSemp2, INFINITE);
	ReleaseSemaphore(g_hSemp2, 1, NULL);
	cout << "thread" << endl;

	return 0;
}

     结果:

main thread

 

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

HANDLE g_hSemp1 = NULL;
HANDLE g_hSemp2 = NULL;

DWORD WINAPI ThreadProc(LPVOID pVoid);

int main()
{
	cout << "main thread" << endl;
	g_hSemp1 = CreateSemaphore(NULL, 1, 10, NULL);
	g_hSemp2 = CreateSemaphore(NULL, 0, 10, NULL);

	HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
	Sleep(1000);
	
	CloseHandle(hThread);
	CloseHandle(g_hSemp1);
	CloseHandle(g_hSemp2);

	return 0;
}

DWORD WINAPI ThreadProc(LPVOID pVoid)
{
	WaitForSingleObject(g_hSemp1, INFINITE);
	ReleaseSemaphore(g_hSemp2, 1, NULL);
	WaitForSingleObject(g_hSemp2, INFINITE);
	cout << "thread" << endl;

	return 0;
}

     结果:

main thread

thread

 

     好了,如果你愿意,你可以自己去变换程序,然后运行,并分析结果,相信对你理解多线程和信号量是有帮助的。


 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值