#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;
}
C++11多线程同步之互斥变量使用学习
最新推荐文章于 2023-04-03 10:02:45 发布