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