Skip to content

Instantly share code, notes, and snippets.

@kuvaldini
Last active October 23, 2021 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuvaldini/d55a3e98bc939fc1eb1844dc46539a49 to your computer and use it in GitHub Desktop.
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.
#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