[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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值