condition-variable

What happens if i call wait on a notified condition variable

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:12:41
Suppose i have two thread and one shared c++ 11 condition variable. what happen if thread1 call notify and after that thread2 call wait? will thread2 block forever or it will continue it's work due to call of notify by thread1? Edit: enum bcLockOperation { bcLockOperation_Light = -1, bcLockOperation_Medium = 50, bcLockOperation_Heavy = 1 } class BC_COREDLL_EXP bcCustomMutex { private: bcCustomMutex(const bcCustomMutex&); bcCustomMutex& operator=(const bcCustomMutex&); protected: bcAtomic<int> mFlag; bcMutex mMutex; bcConditionVariable mCond; public: bcCustomMutex() { bcAtomicOperation:

What happens if i call wait on a notified condition variable

梦想的初衷 提交于 2019-12-01 01:49:11
问题 Suppose i have two thread and one shared c++ 11 condition variable. what happen if thread1 call notify and after that thread2 call wait? will thread2 block forever or it will continue it's work due to call of notify by thread1? Edit: enum bcLockOperation { bcLockOperation_Light = -1, bcLockOperation_Medium = 50, bcLockOperation_Heavy = 1 } class BC_COREDLL_EXP bcCustomMutex { private: bcCustomMutex(const bcCustomMutex&); bcCustomMutex& operator=(const bcCustomMutex&); protected: bcAtomic<int>

CONDITION_VARIABLE in windows; wont compile

孤人 提交于 2019-11-30 20:43:16
I am trying to make a windows-version of a program written for Linux, in C++. For the program to be thread-safe, I use pthread_cond_t and pthread_cond_wait in the Linux version. These functions use a mutex to help make sure that the waiting thread is actually waiting. I found that CONDITION_VARIABLE may do the trick in Windows, however I can't figure out why it wont compile. I get the error "error: 'CONDITION_VARIABLE' does not name a type" even though all relevant headers are included, as far as I can tell. I tried copy-pasting the code on http://msdn.microsoft.com/en-us/library/ms686903%28v

Do I need to synchronize std::condition_variable/condition_variable_any::notify_one

感情迁移 提交于 2019-11-30 07:25:30
问题 Do I need to synchronize std::condition_variable/condition_variable_any::notify_one ? As far as I can see, if lost of notifications is acceptable - it is OK to call notify_one not protected (by mutex for example). For instance, I saw following usage patterns (sorry, don't remember where): { { lock_guard<mutex> l(m); // do work } c.notify_one(); } But, I inspected libstdc++ sources, and I see: condition_variable::notify_one void condition_variable::notify_one() noexcept { int __e = __gthread

CONDITION_VARIABLE in windows; wont compile

社会主义新天地 提交于 2019-11-30 04:56:31
问题 I am trying to make a windows-version of a program written for Linux, in C++. For the program to be thread-safe, I use pthread_cond_t and pthread_cond_wait in the Linux version. These functions use a mutex to help make sure that the waiting thread is actually waiting. I found that CONDITION_VARIABLE may do the trick in Windows, however I can't figure out why it wont compile. I get the error "error: 'CONDITION_VARIABLE' does not name a type" even though all relevant headers are included, as

How do I deal with the system clock changing while waiting on a std::condition_variable?

穿精又带淫゛_ 提交于 2019-11-29 17:08:29
问题 I'm trying to implement some cross-platform code in C++11. Part of this code implements a semaphore object using a std::condition_variable. When I need to do a timed wait on the semaphore, I use wait_until or wait_for. The problem I'm experiencing is that it seems like the standard implementation of condition_variable on POSIX-based systems relies on the system clock, rather than the monotonic clock (see also: this issue against the POSIX spec) That means that if the system clock gets changed

How to guarantee exact thread sleep interval?

霸气de小男生 提交于 2019-11-29 16:40:05
Usually if I want to simulate some work or wait exact time interval I use condition_variable::wait_for or at the worst thread::this_thread::sleep_for . But condition_variable documentation states that wait_for or wait_until methods may block longer than was requested. This function may block for longer than timeout_duration due to scheduling or resource contention delays. How exact wait intervals can be guaranteed? UPDATE Can I reach it without condition_variable ? You cannot do this. In order to have exact guarantees like this, you need a real time operating system. C++ does not guarantee you

Why is there no wait function for condition_variable which does not relock the mutex

吃可爱长大的小学妹 提交于 2019-11-29 07:21:28
Consider the following example. std::mutex mtx; std::condition_variable cv; void f() { { std::unique_lock<std::mutex> lock( mtx ); cv.wait( lock ); // 1 } std::cout << "f()\n"; } void g() { std::this_thread::sleep_for( 1s ); cv.notify_one(); } int main() { std::thread t1{ f }; std::thread t2{ g }; t2.join(); t1.join(); } g() "knows" that f() is waiting in the scenario I would like to discuss. According to cppreference.com there is no need for g() to lock the mutex before calling notify_one . Now in the line marked "1" cv will release the mutex and relock it once the notification is sent. The

Do I need to synchronize std::condition_variable/condition_variable_any::notify_one

本小妞迷上赌 提交于 2019-11-29 03:25:01
Do I need to synchronize std::condition_variable/condition_variable_any::notify_one ? As far as I can see, if lost of notifications is acceptable - it is OK to call notify_one not protected (by mutex for example). For instance, I saw following usage patterns (sorry, don't remember where): { { lock_guard<mutex> l(m); // do work } c.notify_one(); } But, I inspected libstdc++ sources, and I see: condition_variable::notify_one void condition_variable::notify_one() noexcept { int __e = __gthread_cond_signal(&_M_cond); // XXX not in spec // EINVAL if (__e) __throw_system_error(__e); } and condition

POSIX Threads: Condition Variables - what's the point?

回眸只為那壹抹淺笑 提交于 2019-11-29 02:36:39
I've been working with pthreads a fair bit recently and there's one little thing I still don't quite get. I know that condition variables are designed to wait for a specific condition to come true (or be 'signalled'). My question is, how does this differ at all from normal mutexes? From what I understand, aren't condition variables just a mutex with additional logic to unlock another mutex (and lock it again) when the condition becomes true? Psuedocode example: mutex mymutex; condvar mycond; int somevalue = 0; onethread() { lock(mymutex); while(somevalue == 0) cond_wait(mycond, mymutex); if