win32下多线程同步方式之临界区,互斥量,事件对象,信号量
生活随笔
收集整理的這篇文章主要介紹了
win32下多线程同步方式之临界区,互斥量,事件对象,信号量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// win32_thread_syn.cpp : 定義控制臺應用程序的入口點。
//#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;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的win32下多线程同步方式之临界区,互斥量,事件对象,信号量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: boost之asio异步io使用实例
- 下一篇: Linux下多线程同步方式之互斥量,信号