unique_lock加锁release

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

void msg_func()
{
   std::mutex mutex;

   while(1)
   {
      std::unique_lock<std::mutex> unique(mutex);
      std::mutex *pmutex = unique.release();

      std::cout << "do something" << std::endl;

      std::chrono::milliseconds second(1000);
      std::this_thread::sleep_for(second);

      pmutex->unlock();
   }
}

int main()
{
   std::thread mythread(&msg_func);
   getchar();

   return 0;
}
$ g++ unique_lock_release.cpp -std=c++11 -pthread
$ ./a.out 
do something
do something

注:
release释放所有权,需要自己解锁mutex

posted @ 2022-08-07 13:26  thomas_blog  阅读(89)  评论(0编辑  收藏  举报