#include <mutex>
#include <iostream>
class some_big_object
{};
void swap(some_big_object& lhs, some_big_object& rhs)//如果是fixed order交换,容易死锁
{}
class X
{
private:
some_big_object some_detail;
mutable std::mutex m;
public:
X(some_big_object const& sd) :some_detail(sd) {}
friend void swap(X& lhs, X& rhs)
{
if (&lhs == &rhs)
return;
std::lock(lhs.m, rhs.m);
std::lock_guard<std::mutex> lock_a(lhs.m, std::adopt_lock);
std::lock_guard<std::mutex> lock_b(rhs.m, std::adopt_lock);
swap(lhs.some_detail, rhs.some_detail);
}
};
void fuckyou()
{
std::cout << "my name is van" << std::endl;
}
void leatherman()
{
std::cout << "fuck you leather man" << std::endl;
}
int main()
{
std::thread t2(leatherman);//多线程的互相等待也会造成死锁
std::thread t1(fuckyou);
bool a = t1.joinable();
std::cout << a << std::endl;
if (!t1.joinable())
{
t2.join();
}
return 0;
}