From 33393cb858e41c56a201e75bd72e3e3ebb49e149 Mon Sep 17 00:00:00 2001 From: XMuli Date: Thu, 20 Jan 2022 22:15:25 +0800 Subject: [PATCH] feat: ExThreadSafety --- ExThreadSafety/multithread.h | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ExThreadSafety/multithread.h diff --git a/ExThreadSafety/multithread.h b/ExThreadSafety/multithread.h new file mode 100644 index 0000000..8d7a8bf --- /dev/null +++ b/ExThreadSafety/multithread.h @@ -0,0 +1,53 @@ +/******************************************************************* + * Copyright (C) 2022 偕臧 All rights reserved. + * + * Author: 偕臧 + * GitHub: https://github.com/xmuli + * Blog: https://ifmet.cn + * + * Create: 2022.01.12 + * Modify: 2022.01.14 + * File: multithread.h + * Description: 一个多线程竞争的例子,分析出现的“非线程安全”的具体原因,以及两个 + * 解决方法:atomic 和 mutex + * + ******************************************************************/ +#ifndef MULTITHREAD_H +#define MULTITHREAD_H + +#include +#include +#include +#include +using namespace std; + +// 方案一:定义为原子变量 atomic +//atomic g_num = 0; + +// 方案二:加锁 mutex +int g_num = 0; +mutex g_mutex; + +void addition() +{ + for (int i = 0; i < 5000; ++i) { + g_mutex.lock(); // 尝试和下一行的 cout 行互换位置 + cout << "threadId:" << this_thread::get_id() << " g_num:" << g_num << endl; + g_num++; + g_mutex.unlock(); + } +} + +使用示例 +int main() +{ + thread th1(addition); + thread th2(addition); + th1.join(); + th2.join(); + + cout << "g_num:" << g_num << endl; + return 0; +} + +#endif \ No newline at end of file