其他分享
首页 > 其他分享> > unique_lock加锁adopt_lock

unique_lock加锁adopt_lock

作者:互联网

#include <iostream>
#include <mutex>
#include <thread>

std::mutex mutex;

void msg_func()
{
   mutex.lock();
   std::unique_lock<std::mutex> unique(mutex, std::adopt_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.cpp -std=c++11 -pthread
$ ./a.out 
do something
do something

注:
adopt_lock表示这个互斥量已经被lock了

标签:std,加锁,lock,adopt,include,mutex,unique
来源: https://www.cnblogs.com/zhangxuechao/p/16558875.html