Last active
October 23, 2021 17:33
-
-
Save kuvaldini/d55a3e98bc939fc1eb1844dc46539a49 to your computer and use it in GitHub Desktop.
Without lambda works as expected, but with lambda look more like spin lock.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
#include <chrono> | |
#include <condition_variable> | |
using namespace std; | |
using namespace std::chrono; | |
using namespace std::chrono_literals; | |
std::mutex expiration_thread_work_mutex_; | |
std::atomic_flag continue_work_flag_(true); | |
std::condition_variable expiration_thread_work_cv_; | |
int main() { | |
std::thread expiration_thread_([]() { | |
while (1) { | |
std::unique_lock lk(expiration_thread_work_mutex_); | |
cout << "!!! dododod" << endl; | |
if (expiration_thread_work_cv_.wait_for( | |
lk, 1s///)==std::cv_status::timeout) | |
, [] { | |
cout<<"!!! test()"<<endl; | |
return continue_work_flag_.test(); | |
})) | |
{ | |
cout << "!!! continue" << endl; | |
continue; | |
} else { | |
cout << "!!! break" << endl; | |
break; | |
} | |
} | |
}); | |
std::this_thread::sleep_for(10s); | |
cout << "!!! ~FairMstProcessor" << endl; | |
continue_work_flag_.clear(); | |
expiration_thread_work_cv_.notify_all(); | |
if (expiration_thread_.joinable()) | |
expiration_thread_.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment