其他分享
首页 > 其他分享> > lock_guard加锁

lock_guard加锁

作者:互联网

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

std::mutex mutex;

void msg_func()
{
   std::lock_guard<std::mutex> guard(mutex);

   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++ lock_guard.cpp -std=c++11 -pthread
$ ./a.out 
do something
do something

标签:std,do,加锁,lock,guard,mutex,include
来源: https://www.cnblogs.com/zhangxuechao/p/16558817.html