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

来源:互联网 发布:java高并发项目源码 编辑:程序博客网 时间:2024/06/02 05:02
#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;}

0 0
原创粉丝点击