#include <thread>
#include <Windows.h>
#include <mutex>
mutex mu;
int totalNum = 10;
using namespace std;
void thread01(int num)
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working !" << endl;
Sleep(100);
mu.lock(); //同步数据锁
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock(); //解除锁定
}
}
void thread02()
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 02 is working !" << endl;
Sleep(200);
}
}
int main()
{
std::cout << "Hello World!\n";
thread task01(thread01, 5);
thread task02(thread02);
//task01.join();
//task02.join();
task01.detach();
task02.detach();
for (int i = 0; i < 5; i++)
{
cout << "Main thread is working !" << endl;
Sleep(200);
}
system("pause");
}
C++ 线程
最新推荐文章于 2024-10-12 21:07:13 发布