condition-variable

condition_variable workaround for wait_until with system time change

瘦欲@ 提交于 2021-02-07 10:44:07
问题 I have a timer class which uses the std::condition_variable wait_until (I have also tried wait_for ). I am using the std::chrono::steady_clock time to wait until a specific time in the future. This is meant to be monotonic, but there has been a long standing issue with this where this actually uses the system clock and fails to work correctly when the system time is changed. It has been fixed in libc as suggested here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=41861. the issue is that this

predicate for condition variable

心已入冬 提交于 2021-02-04 19:10:10
问题 I am new to multi threading. While writing multi threaded code in C++11 using condition variable , I use the following construct while(predicate) { cond_var.wait(&lock); } However, I have been reading Deitel's third edition book on operating systems(chp 6) where the following construct is being used if(predicate) { cond_var.wait(&lock); } So, what's the difference? Why isn't the book using while? Isn't spurious call an issue? 回答1: Spurious wakeup is always a potential issue. For example, look

predicate for condition variable

耗尽温柔 提交于 2021-02-04 19:09:08
问题 I am new to multi threading. While writing multi threaded code in C++11 using condition variable , I use the following construct while(predicate) { cond_var.wait(&lock); } However, I have been reading Deitel's third edition book on operating systems(chp 6) where the following construct is being used if(predicate) { cond_var.wait(&lock); } So, what's the difference? Why isn't the book using while? Isn't spurious call an issue? 回答1: Spurious wakeup is always a potential issue. For example, look

predicate for condition variable

对着背影说爱祢 提交于 2021-02-04 19:07:38
问题 I am new to multi threading. While writing multi threaded code in C++11 using condition variable , I use the following construct while(predicate) { cond_var.wait(&lock); } However, I have been reading Deitel's third edition book on operating systems(chp 6) where the following construct is being used if(predicate) { cond_var.wait(&lock); } So, what's the difference? Why isn't the book using while? Isn't spurious call an issue? 回答1: Spurious wakeup is always a potential issue. For example, look

what happen between after notify_all() and before wait() get the lock?

≯℡__Kan透↙ 提交于 2021-01-29 08:34:49
问题 I test the std::condition_variable using the below code: class CondWait{ public: std::condition_variable cv; std::mutex mu; int i=0; public: void mainTask(){ std::unique_lock<std::mutex> lk(mu); cv.wait(lk); i++; std::cout<<"main task, "<<i<<std::endl; } void notifyTask(){ std::unique_lock<std::mutex> lk(mu); i = 0; std::cout<<"notify task, "<<i<<std::endl; cv.notify_one(); std::cout<<"notify task, sleep 5 sec"<<std::endl; std::this_thread::sleep_for(std::chrono::seconds(5)); } }; int main()

Why this example using mutex is less efficient compared to another one with additional condition variable?

半腔热情 提交于 2021-01-28 11:01:42
问题 An example from The Linux Programming Interface : In the producer threads, we would have code such as the following: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static int avail = 0; /* Code to produce a unit omitted */ s = pthread_mutex_lock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_lock"); avail++; /* Let consumer know another unit is available */ s = pthread_mutex_unlock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_unlock"); And in the main (consumer) thread, we could

What if the system time changes while I'm doing timed_wait with a duration?

别等时光非礼了梦想. 提交于 2021-01-27 03:54:19
问题 When using timed_wait on a boost::condition_variable with a duration, will the wait condition time out after the duration even if the user (or ntp) changes the system time? E.g., boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1 sec // ** System time jumps back 15 minutes here. ** if( !signal.timed_wait(lock, wait_duration) ) { // Does this condition happen 1 second later, or about 15 minutes later? } 回答1: As of the date of writing (Nov 2013), if the wall-clock time changes

What if the system time changes while I'm doing timed_wait with a duration?

北城余情 提交于 2021-01-27 03:53:24
问题 When using timed_wait on a boost::condition_variable with a duration, will the wait condition time out after the duration even if the user (or ntp) changes the system time? E.g., boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1 sec // ** System time jumps back 15 minutes here. ** if( !signal.timed_wait(lock, wait_duration) ) { // Does this condition happen 1 second later, or about 15 minutes later? } 回答1: As of the date of writing (Nov 2013), if the wall-clock time changes

What if the system time changes while I'm doing timed_wait with a duration?

强颜欢笑 提交于 2021-01-27 03:52:38
问题 When using timed_wait on a boost::condition_variable with a duration, will the wait condition time out after the duration even if the user (or ntp) changes the system time? E.g., boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1 sec // ** System time jumps back 15 minutes here. ** if( !signal.timed_wait(lock, wait_duration) ) { // Does this condition happen 1 second later, or about 15 minutes later? } 回答1: As of the date of writing (Nov 2013), if the wall-clock time changes

Difference between std::atomic and std::condition_variable wait, notify_* methods

我是研究僧i 提交于 2020-12-30 06:01:53
问题 I was looking through 'Atomic operations library' and came across a new c++20 feature of atomic 'wait' and 'notify_ ' methods. I am curious on what the differences are in regards to std::condition_variable's 'wait' and 'notify_ ' methods. 回答1: std:atomic wait , notify_all and notify_one methods are similar to methods of conditional variables. They allow the implementation of the logic that previously required conditional variable by using much more efficient and lightweight atomic variables.