#include &l..._线程c++ demo">
当前位置:   article > 正文

[C++] 线程同步的四种方式和demo_线程c++ demo

线程c++ demo

源码路径:dangwei-90/ThreadSync: 线程同步 (github.com)    (https://github.com/dangwei-90/ThreadSync)

编译平台:win10
编译工具:vs2019
语音:       C++

线程同步的四种方式:
临界区
事件
信号
互斥量

 

下边直接看demo,一目了然:

1.线程未同步的demo

  1. // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include <iostream>
  4. #include <thread>
  5. #include <windows.h>
  6. int g_num = 0;
  7. void func_1() {
  8. while (g_num < 20) {
  9. ++g_num;
  10. Sleep(100);
  11. std::cout << "func_1 g_num:" << g_num << std::endl;
  12. }
  13. }
  14. void func_2() {
  15. while (g_num < 20) {
  16. ++g_num;
  17. Sleep(100);
  18. std::cout << "func_2 g_num:" << g_num << std::endl;
  19. }
  20. }
  21. int main()
  22. {
  23. std::thread thread_1(func_1);
  24. std::thread thread_2(func_2);
  25. if (thread_1.joinable()) {
  26. thread_1.join();
  27. }
  28. if (thread_2.joinable()) {
  29. thread_2.join();
  30. }
  31. return 0;
  32. }

输出:

2.临界区 demo

  1. // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include <iostream>
  4. #include <thread>
  5. #include <windows.h>
  6. CRITICAL_SECTION g_critical; //定义临界区句柄
  7. int g_num_section = 0;
  8. void func_1_section() {
  9. while (g_num_section < 20) {
  10. EnterCriticalSection(&g_critical);
  11. ++g_num_section;
  12. Sleep(100);
  13. std::cout << "func_1 g_num:" << g_num_section << std::endl;
  14. LeaveCriticalSection(&g_critical);
  15. }
  16. }
  17. void func_2_section() {
  18. while (g_num_section < 20) {
  19. EnterCriticalSection(&g_critical);
  20. ++g_num_section;
  21. Sleep(100);
  22. std::cout << "func_2 g_num:" << g_num_section << std::endl;
  23. LeaveCriticalSection(&g_critical);
  24. }
  25. }
  26. int main()
  27. {
  28. InitializeCriticalSection(&g_critical);
  29. std::thread thread_1(func_1_section);
  30. std::thread thread_2(func_2_section);
  31. if (thread_1.joinable()) {
  32. thread_1.join();
  33. }
  34. if (thread_2.joinable()) {
  35. thread_2.join();
  36. }
  37. return 0;
  38. }

3.事件 demo

  1. // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include <iostream>
  4. #include <thread>
  5. #include <windows.h>
  6. HANDLE g_event = nullptr;
  7. int g_num_event = 0;
  8. void func_1_event() {
  9. while (g_num_event < 20) {
  10. WaitForSingleObject(g_event, INFINITE);
  11. ++g_num_event;
  12. Sleep(100);
  13. std::cout << "func_1 g_num:" << g_num_event << std::endl;
  14. SetEvent(g_event);
  15. }
  16. }
  17. void func_2_event() {
  18. while (g_num_event < 20) {
  19. WaitForSingleObject(g_event, INFINITE);
  20. ++g_num_event;
  21. Sleep(100);
  22. std::cout << "func_2 g_num:" << g_num_event << std::endl;
  23. SetEvent(g_event);
  24. }
  25. }
  26. int main()
  27. {
  28. g_event = CreateEvent(NULL, FALSE, TRUE, L"thread_sync_event");
  29. std::thread thread_1(func_1_event);
  30. std::thread thread_2(func_2_event);
  31. if (thread_1.joinable()) {
  32. thread_1.join();
  33. }
  34. if (thread_2.joinable()) {
  35. thread_2.join();
  36. }
  37. return 0;
  38. }

4.信号量 demo

  1. // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include <iostream>
  4. #include <thread>
  5. #include <windows.h>
  6. HANDLE g_semaphore = nullptr;
  7. int g_num_semaphore = 0;
  8. void func_1_semaphore() {
  9. while (g_num_semaphore < 20) {
  10. WaitForSingleObject(g_semaphore, INFINITE);
  11. ++g_num_semaphore;
  12. Sleep(100);
  13. std::cout << "func_1 g_num:" << g_num_semaphore << std::endl;
  14. long lcount = 0;
  15. ReleaseSemaphore(g_semaphore, 1, &lcount);
  16. }
  17. }
  18. void func_2_semaphore() {
  19. while (g_num_semaphore < 20) {
  20. WaitForSingleObject(g_semaphore, INFINITE);
  21. ++g_num_semaphore;
  22. Sleep(100);
  23. std::cout << "func_2 g_num:" << g_num_semaphore << std::endl;
  24. long lcount = 0;
  25. ReleaseSemaphore(g_semaphore, 1, &lcount);
  26. }
  27. }
  28. int main()
  29. {
  30. g_semaphore = CreateSemaphore(NULL, 1, 100, L"thread_sync_semaphore");
  31. std::thread thread_1(func_1_semaphore);
  32. std::thread thread_2(func_2_semaphore);
  33. if (thread_1.joinable()) {
  34. thread_1.join();
  35. }
  36. if (thread_2.joinable()) {
  37. thread_2.join();
  38. }
  39. return 0;
  40. }

5.互斥量 demo

  1. // thread_sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3. #include <iostream>
  4. #include <thread>
  5. #include <windows.h>
  6. HANDLE g_mutex = nullptr;
  7. int g_num_mutex = 0;
  8. void func_1_mutex() {
  9. while (g_num_mutex < 20) {
  10. WaitForSingleObject(g_mutex, INFINITE);
  11. ++g_num_mutex;
  12. Sleep(100);
  13. std::cout << "func_1 g_num:" << g_num_mutex << std::endl;
  14. long lcount = 0;
  15. ReleaseMutex(g_mutex);
  16. }
  17. }
  18. void func_2_mutex() {
  19. while (g_num_mutex < 20) {
  20. WaitForSingleObject(g_mutex, INFINITE);
  21. ++g_num_mutex;
  22. Sleep(100);
  23. std::cout << "func_2 g_num:" << g_num_mutex << std::endl;
  24. long lcount = 0;
  25. ReleaseMutex(g_mutex);
  26. }
  27. }
  28. int main()
  29. {
  30. g_mutex = CreateMutex(NULL, false, L"thread_sync_mutex");
  31. std::thread thread_1(func_1_mutex);
  32. std::thread thread_2(func_2_mutex);
  33. if (thread_1.joinable()) {
  34. thread_1.join();
  35. }
  36. if (thread_2.joinable()) {
  37. thread_2.join();
  38. }
  39. return 0;
  40. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号