#include "stdafx.h" #include "iostream" #include "list" #include "windows.h" #include "process.h" using namespace std; /* 線程同步 1.臨界區 2.信號量 3.事件對象 4.互斥量 */ /************************************************************************/ /* 臨界區 單進程內線程同步 */ /************************************************************************/ CRITICAL_SECTION g_cri; //臨界區 list<int> g_list2; unsigned int __stdcall threadRead(LPVOID param) { while(true) { EnterCriticalSection(&g_cri); for (list<int>::iterator it = g_list2.begin(); it != g_list2.end();it++) { cout << "元素為:"<<*it << endl; } LeaveCriticalSection(&g_cri); Sleep(100); } return 0; } unsigned int __stdcall threadWrite(LPVOID param) { srand(GetTickCount()); while (true) { EnterCriticalSection(&g_cri); if (g_list2.size()>0) { if (rand() % 100 > 50) { g_list2.push_back(rand()); } else { g_list2.erase(g_list2.begin()); } } else { g_list2.push_back(rand()); } LeaveCriticalSection(&g_cri); Sleep(100); } return 0; } HANDLE g_mutex; //互斥量 unsigned int times = 0; /************************************************************************/ /* 互斥量 */ /************************************************************************/ unsigned int __stdcall firstThread(LPVOID param) { while (true) { WaitForSingleObject(g_mutex, INFINITE); if (times <=20) { cout << "線程1的第" << times++ << "次.." << endl; } else { break; } ReleaseMutex(g_mutex); } return 0; } unsigned int __stdcall secondThread(LPVOID param) { while (true) { WaitForSingleObject(g_mutex, INFINITE); if (times <= 20) { Sleep(1); cout << "線程2的第" << times++ << "次.." << endl; } else { break; } ReleaseMutex(g_mutex); } return 0; } /************************************************************************/ /* 事件對象 可對進程外的線程同步 CreateEvent(NULL, false, false, NULL); setEvent(g_event); */ /************************************************************************/ HANDLE g_event; unsigned int __stdcall firstThread2(LPVOID param) { while (true) { WaitForSingleObject(g_event, INFINITE); if (times <= 20) { cout << "線程1的第" << times++ << "次.." << endl; } else { break; } SetEvent(g_event); } return 0; } unsigned int __stdcall secondThread2(LPVOID param) { while (true) { WaitForSingleObject(g_event, INFINITE); if (times <= 20) { Sleep(1); cout << "線程2的第" << times++ << "次.." << endl; } else { break; } SetEvent(g_event); } return 0; } /************************************************************************/ /* 信號量 主要是可以指定個數 允許多個線程同一時刻訪問同一資源,我們可以指定允許個數 */ /************************************************************************/ HANDLE g_semaphore; unsigned int __stdcall firstThread3(LPVOID param) { while (true) { WaitForSingleObject(g_semaphore, INFINITE); if (times <= 20) { cout << "線程1的第" << times++ << "次.." << endl; } else { break; } ReleaseSemaphore(g_semaphore, 1, NULL); } return 0; } unsigned int __stdcall secondThread3(LPVOID param) { while (true) { WaitForSingleObject(g_semaphore, INFINITE); if (times <= 20) { Sleep(1); cout << "線程2的第" << times++ << "次.." << endl; } else { break; } ReleaseSemaphore(g_semaphore, 1, NULL); } return 0; } int _tmain(int argc, _TCHAR* argv[]) { //InitializeCriticalSection(&g_cri); //初始化臨界區 //g_mutex = CreateMutex(NULL, false, NULL); //創建互斥量 //g_event = CreateEvent(NULL, false, false, NULL); //可以根據第四個參數(線程名稱)進行,不同進程的線程同步 //SetEvent(g_event); g_semaphore = CreateSemaphore(NULL, 1, 1, NULL); //信號量 HANDLE g_thread1 = (HANDLE)_beginthreadex(NULL, 0, firstThread3, NULL, 0, NULL); HANDLE g_thread2 = (HANDLE)_beginthreadex(NULL, 0, secondThread3, NULL, 0, NULL); CloseHandle(g_thread1); CloseHandle(g_thread2); cout << "主線程..\n"; Sleep(400000); system("pause"); return 0; }
