C++11多线程同步之互斥变量使用学习

xiaoxiao2021-02-28  102

#include <iostream> #include <thread> #include <string> #include <mutex> using namespace std; std::mutex g_mutex; volatile int g_count(0); void ThreadFunc(int i, double d, const string &s) { for (size_t i = 0; i < 100; i++) { g_mutex.lock(); g_count++; cout << "[start " << " " << i << " " << d << " " << s << " end]" << endl; g_mutex.unlock(); } } int main() { thread thread1(ThreadFunc, 1, 1.11111, "sample01"); thread thread2(ThreadFunc, 2, 2.22222, "sample02"); thread1.join(); thread2.join(); cout << endl << "g_count = " << g_count << endl; system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-59939.html

最新回复(0)