1. 程式人生 > >c++ std::thread + lambda 實現計時器

c++ std::thread + lambda 實現計時器

wake aps pen hide view color col pre pro

技術分享圖片
 1 bool wait_for_wake = false;
 2 std::mutex process_mutex;
 3 std::condition_variable_any process_cond;
 4 std::unique_lock<std::mutex> lock(process_mutex);
 5 auto Timer = [&process_mutex, &process_cond](const int &wait_time) {
 6   std::this_thread::sleep_for(std::chrono::milliseconds(wait_time));
7 process_cond.notify_one(); 8 }; 9 10 // use 11 auto wait_time = 50; 12 std::thread(Timer, wait_time).detach(); 13 wait_for_wake = true; 14 15 while (wait_for_wake) { 16 process_cond.wait(lock); 17 wait_for_wake = false; 18 }
View Code

c++ std::thread + lambda 實現計時器