转自:http://download.csdn.net/down/2887013/virgil0236
//线程同步-互斥量
#include <Windows.h>
#include <process.h>
#include <iostream>
using namespace std;
const int G_THREAD_INSTANCE_NUMBER = 3;
unsigned __stdcall ThreadProc(LPVOID pData);
int main()
{
unsigned ID[G_THREAD_INSTANCE_NUMBER];
HANDLE h[G_THREAD_INSTANCE_NUMBER];
HANDLE hMutex;
hMutex = CreateMutex(NULL, FALSE, TEXT("MutexTest"));
if(!hMutex) {
cout << "Create Mutex error!" << endl;
return 0;
}
for(int i = 0; i < G_THREAD_INSTANCE_NUMBER; ++i) {
h[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, (void*)&ID[i], 0, &(ID[i]));
Sleep(1000);
}
WaitForMultipleObjects(G_THREAD_INSTANCE_NUMBER, h, TRUE, INFINITE);
for(int i = 0; i < G_THREAD_INSTANCE_NUMBER; ++i){
CloseHandle(h[i]);
}
cout << "Close the mutex handle!" << endl;
CloseHandle(hMutex);
}
unsigned __stdcall ThreadProc(LPVOID pData)
{
int nThreadNumberTemp = (*(int*)pData);
HANDLE hMutex;
cout << "ThreadProc:" << nThreadNumberTemp << "is running!" << endl;
hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, TEXT("MutexTest"));
if(!hMutex) {
cout << "Open Mutex error!" << endl;
}
else {
cout << "ThreadProc" << nThreadNumberTemp << "gets the mutex/n" << endl;
ReleaseMutex(hMutex);
}
CloseHandle(hMutex);
return 0;
}
//线程同步-临界区
#include <Windows.h>
#include <process.h>
#include <iostream>
using namespace std;
int g_total = 100;
HANDLE g_evFin[2];
CRITICAL_SECTION cs;
unsigned __stdcall WithdrawThread1(LPVOID param);
unsigned __stdcall WithdrawThread2(LPVOID param);
int main()
{
g_evFin[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
g_evFin[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
HANDLE handle1;
HANDLE handle2;
InitializeCriticalSection(&cs);
handle1 = (HANDLE)_beginthreadex(NULL, 0, WithdrawThread1, NULL, 0, NULL);
handle2 = (HANDLE)_beginthreadex(NULL, 0, WithdrawThread2, NULL, 0, NULL);
WaitForMultipleObjects(2, g_evFin, TRUE, INFINITE);
cout << g_total << endl;
DeleteCriticalSection(&cs);
CloseHandle(g_evFin[0]);
CloseHandle(g_evFin[1]);
CloseHandle(handle1);
CloseHandle(handle2);
}
unsigned __stdcall WithdrawThread1(LPVOID param)
{
EnterCriticalSection(&cs);
if(g_total-90 >= 0) {
g_total -= 90;
cout << "You withdraw 90" << endl;
}
else
cout << "You do not have that much money" << endl;
LeaveCriticalSection(&cs);
SetEvent(g_evFin[0]);
return 0;
}
unsigned __stdcall WithdrawThread2(LPVOID param)
{
EnterCriticalSection(&cs);
if(g_total-20 >= 0) {
g_total -= 20;
cout << "You withdraw 20" << endl;
}
else
cout << "You do not have that much money" << endl;
LeaveCriticalSection(&cs);
SetEvent(g_evFin[1]);
return 0;
}
//线程同步-全局变量
//该方法不安全!只为演示用
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;
bool g_bVal = false;
unsigned __stdcall ThreadFunc(LPVOID pParam);
int main()
{
HANDLE handle = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, NULL, 0, NULL);
if(!handle) {
cout << "Thread create error!" << endl;
CloseHandle(handle);
}
while(!g_bVal)
cout << "Thread while" << endl;
cout << "thread exit" << endl;
CloseHandle(handle);
}
unsigned __stdcall ThreadFunc(LPVOID pParam)
{
cout << "ThreadFunc" << endl;
Sleep(200);
g_bVal = true;
return 0;
}
//线程同步-事件
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;
HANDLE evRead;
HANDLE evFinish;
unsigned __stdcall ReadThread(LPVOID param);
unsigned __stdcall WINAPI WriteThread(LPVOID param);
int main()
{
evRead = CreateEvent(NULL, FALSE, FALSE, NULL);
evFinish = CreateEvent(NULL, FALSE, FALSE, NULL);
HANDLE handle1;
HANDLE handle2;
handle1 = (HANDLE)_beginthreadex(NULL, 0, ReadThread, NULL, 0, NULL);
handle2 = (HANDLE)_beginthreadex(NULL, 0, WriteThread, NULL, 0, NULL);
WaitForSingleObject(evFinish, INFINITE);
cout << "The program is end" << endl;
CloseHandle(handle1);
CloseHandle(handle2);
CloseHandle(evRead);
CloseHandle(evFinish);
}
unsigned __stdcall ReadThread(LPVOID param)
{
WaitForSingleObject(evRead, INFINITE);
cout << "Reading" << endl;
SetEvent(evFinish);
return 0;
}
unsigned __stdcall WriteThread(LPVOID param)
{
cout << "Writing" << endl;
SetEvent(evRead);
return 0;
}
//线程同步-信号量
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;
#define THREAD_INSTANCE_NUMBER 3
unsigned __stdcall foo(void* pData);
int main()
{
unsigned nThreadID[THREAD_INSTANCE_NUMBER];
HANDLE threads[THREAD_INSTANCE_NUMBER];
HANDLE hSemaphore;
hSemaphore = CreateSemaphore(NULL, 0, 1, TEXT("Semaphore.Test"));
if(!hSemaphore) {
cout << "Create semaphore error!" << endl;
return 0;
}
for(int i = 0; i < THREAD_INSTANCE_NUMBER; ++i) {
threads[i] = (HANDLE)_beginthreadex(NULL, 0, foo, (void*)&nThreadID[i], 0, &(nThreadID[i]));
Sleep(1000);
}
WaitForMultipleObjects(THREAD_INSTANCE_NUMBER, (HANDLE*)threads, TRUE, INFINITE);
cout << "Close the semaphore handle!" << endl;
for(int i = 0; i < THREAD_INSTANCE_NUMBER; ++i)
CloseHandle(threads[i]);
CloseHandle(hSemaphore);
}
unsigned __stdcall foo(void* pData)
{
int nThreadNumberTemp = (*(int*)pData);
HANDLE hSemaphore;
cout << "foo:" << nThreadNumberTemp << "is running!" << endl;
hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, TEXT("Semaphore.Test"));
if(!hSemaphore)
cout << "Open semaphore error!" << endl;
cout << "foo" << nThreadNumberTemp << "gets the semaphore/n" << endl;
ReleaseSemaphore(hSemaphore, 1, NULL);
CloseHandle(hSemaphore);
return 0;
}