unique_lock加锁defer_lock
作者:互联网
#include <iostream>
#include <mutex>
#include <thread>
std::mutex mutex;
void msg_func()
{
std::unique_lock<std::mutex> unique(mutex, std::defer_lock);
unique.lock();
while(1)
{
std::cout << "do something" << std::endl;
std::chrono::milliseconds second(1000);
std::this_thread::sleep_for(second);
}
}
int main()
{
std::thread mythread(&msg_func);
getchar();
return 0;
}
$ g++ unique_lock_defer.cpp -std=c++11 -pthread
$ ./a.out
do something
do something
注:
defer_lock创建一个没有加锁的mutex
标签:std,defer,加锁,lock,include,mutex,unique 来源: https://www.cnblogs.com/zhangxuechao/p/16558925.html