#include &l..._线程c++ demo">
赞
踩
源码路径:dangwei-90/ThreadSync: 线程同步 (github.com) (https://github.com/dangwei-90/ThreadSync)
编译平台:win10
编译工具:vs2019
语音: C++
线程同步的四种方式:
临界区
事件
信号
互斥量
下边直接看demo,一目了然:
1.线程未同步的demo
- // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- #include <thread>
- #include <windows.h>
-
- int g_num = 0;
-
- void func_1() {
- while (g_num < 20) {
- ++g_num;
- Sleep(100);
-
- std::cout << "func_1 g_num:" << g_num << std::endl;
- }
- }
-
- void func_2() {
- while (g_num < 20) {
- ++g_num;
- Sleep(100);
-
- std::cout << "func_2 g_num:" << g_num << std::endl;
- }
- }
-
- int main()
- {
- std::thread thread_1(func_1);
- std::thread thread_2(func_2);
-
- if (thread_1.joinable()) {
- thread_1.join();
- }
- if (thread_2.joinable()) {
- thread_2.join();
- }
-
- return 0;
- }

输出:
2.临界区 demo
- // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- #include <thread>
- #include <windows.h>
-
- CRITICAL_SECTION g_critical; //定义临界区句柄
- int g_num_section = 0;
-
- void func_1_section() {
- while (g_num_section < 20) {
- EnterCriticalSection(&g_critical);
- ++g_num_section;
- Sleep(100);
-
- std::cout << "func_1 g_num:" << g_num_section << std::endl;
- LeaveCriticalSection(&g_critical);
- }
- }
-
- void func_2_section() {
- while (g_num_section < 20) {
- EnterCriticalSection(&g_critical);
- ++g_num_section;
- Sleep(100);
-
- std::cout << "func_2 g_num:" << g_num_section << std::endl;
- LeaveCriticalSection(&g_critical);
- }
- }
-
- int main()
- {
- InitializeCriticalSection(&g_critical);
- std::thread thread_1(func_1_section);
- std::thread thread_2(func_2_section);
-
- if (thread_1.joinable()) {
- thread_1.join();
- }
- if (thread_2.joinable()) {
- thread_2.join();
- }
-
- return 0;
- }

3.事件 demo
- // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- #include <thread>
- #include <windows.h>
-
- HANDLE g_event = nullptr;
- int g_num_event = 0;
-
- void func_1_event() {
- while (g_num_event < 20) {
- WaitForSingleObject(g_event, INFINITE);
- ++g_num_event;
- Sleep(100);
-
- std::cout << "func_1 g_num:" << g_num_event << std::endl;
- SetEvent(g_event);
- }
- }
-
- void func_2_event() {
- while (g_num_event < 20) {
- WaitForSingleObject(g_event, INFINITE);
- ++g_num_event;
- Sleep(100);
-
- std::cout << "func_2 g_num:" << g_num_event << std::endl;
- SetEvent(g_event);
- }
- }
-
- int main()
- {
- g_event = CreateEvent(NULL, FALSE, TRUE, L"thread_sync_event");
- std::thread thread_1(func_1_event);
- std::thread thread_2(func_2_event);
-
- if (thread_1.joinable()) {
- thread_1.join();
- }
- if (thread_2.joinable()) {
- thread_2.join();
- }
-
- return 0;
- }

4.信号量 demo
- // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- #include <thread>
- #include <windows.h>
-
- HANDLE g_semaphore = nullptr;
- int g_num_semaphore = 0;
-
- void func_1_semaphore() {
- while (g_num_semaphore < 20) {
- WaitForSingleObject(g_semaphore, INFINITE);
- ++g_num_semaphore;
- Sleep(100);
-
- std::cout << "func_1 g_num:" << g_num_semaphore << std::endl;
- long lcount = 0;
- ReleaseSemaphore(g_semaphore, 1, &lcount);
- }
- }
-
- void func_2_semaphore() {
- while (g_num_semaphore < 20) {
- WaitForSingleObject(g_semaphore, INFINITE);
- ++g_num_semaphore;
- Sleep(100);
-
- std::cout << "func_2 g_num:" << g_num_semaphore << std::endl;
- long lcount = 0;
- ReleaseSemaphore(g_semaphore, 1, &lcount);
- }
- }
-
- int main()
- {
- g_semaphore = CreateSemaphore(NULL, 1, 100, L"thread_sync_semaphore");
- std::thread thread_1(func_1_semaphore);
- std::thread thread_2(func_2_semaphore);
-
- if (thread_1.joinable()) {
- thread_1.join();
- }
- if (thread_2.joinable()) {
- thread_2.join();
- }
-
- return 0;
- }

5.互斥量 demo
- // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- #include <thread>
- #include <windows.h>
-
- HANDLE g_mutex = nullptr;
- int g_num_mutex = 0;
-
- void func_1_mutex() {
- while (g_num_mutex < 20) {
- WaitForSingleObject(g_mutex, INFINITE);
- ++g_num_mutex;
- Sleep(100);
-
- std::cout << "func_1 g_num:" << g_num_mutex << std::endl;
- long lcount = 0;
- ReleaseMutex(g_mutex);
- }
- }
-
- void func_2_mutex() {
- while (g_num_mutex < 20) {
- WaitForSingleObject(g_mutex, INFINITE);
- ++g_num_mutex;
- Sleep(100);
-
- std::cout << "func_2 g_num:" << g_num_mutex << std::endl;
- long lcount = 0;
- ReleaseMutex(g_mutex);
- }
- }
-
- int main()
- {
- g_mutex = CreateMutex(NULL, false, L"thread_sync_mutex");
- std::thread thread_1(func_1_mutex);
- std::thread thread_2(func_2_mutex);
-
- if (thread_1.joinable()) {
- thread_1.join();
- }
- if (thread_2.joinable()) {
- thread_2.join();
- }
-
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。