#include <iostream>
#include <string>
#include "httplib.h"
using namespace std;
int i = 0;
std::mutex obj;
void run1(int a){
while (1) {
Sleep(400);
std::unique_lock<std::mutex> ni(obj);
std::unique_lock<std::mutex> nj(std::move(ni));
i++;
std::cout << i << std::endl;
std::cout << "子线程ID = " << std::this_thread::get_id() << std::endl;
}
}
void run2(int a) {
while (1) {
Sleep(400);
std::unique_lock<std::mutex> ni(obj);
i--;
std::cout << i << std::endl;
std::cout << "子线程ID = " << std::this_thread::get_id() << std::endl;
}
}
int main()
{
std::thread o1(run1, 1);
std::thread o2(run2, 1);
o1.join();
o2.join();
std::cout << "主线程ID = " << std::this_thread::get_id() << std::endl;
return 0;
}
|