#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <thread>
std::mutex mtx;
void func2() {
mtx.lock();
std::cout << "here is 2" << std::endl;
mtx.unlock();
}
void func1() {
mtx.lock();
std::cout << "here is 1" << std::endl;
func2();
mtx.unlock();
}
int main() {
func1();
}
但如果我将主函数修改如下,它会引起死锁
int main() {
std::thread t1(func1);
t1.join();
}
我用 "g++ test.cpp -std=c++11 -lpthread
" 编译了这两个文件。