condition-variable

What is the difference between busy-wait and polling?

て烟熏妆下的殇ゞ 提交于 2019-11-28 20:50:09
From the Wikipedia article on Polling Polling, or polled operation, in computer science, refers to actively sampling the status of an external device by a client program as a synchronous activity. Polling is most often used in terms of input/output (I/O), and is also referred to as polled I/O or software driven I/O. Polling is sometimes used synonymously with busy-wait polling (busy waiting). In this situation, when an I/O operation is required the computer does nothing other than check the status of the I/O device until it is ready, at which point the device is accessed. In other words the

What is the best way to wait on multiple condition variables in C++11?

我与影子孤独终老i 提交于 2019-11-28 20:21:40
First a little context : I'm in the process of learning about threading in C++11 and for this purpose, I'm trying to build a small actor class, essentially (I left the exception handling and propagation stuff out) like so: class actor { private: std::atomic<bool> stop; private: std::condition_variable interrupt; private: std::thread actor_thread; private: message_queue incoming_msgs; public: actor() : stop(false), actor_thread([&]{ run_actor(); }) {} public: virtual ~actor() { // if the actor is destroyed, we must ensure the thread dies too stop = true; // to this end, we have to interrupt the

std::condition_variable - Wait for several threads to notify observer

安稳与你 提交于 2019-11-28 11:44:59
问题 my problem looks like this: I've got a observer which holds a std::condition_variable and a std::mutex, my worker thread objects have a pointer to the observer. Each time a worker thread finished its job it calls m_Observer->NotifyOne() which then calls the notify_one() function of the condition_variable. Now what i want to do is, start a bunch of worker threads each with a different job and different (independant) data and wait for all of them to signal (using m_Observer->NotifyOne()) the

How to guarantee exact thread sleep interval?

自古美人都是妖i 提交于 2019-11-28 11:30:01
问题 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 ? 回答1: You cannot do this. In

condition variable [closed]

£可爱£侵袭症+ 提交于 2019-11-28 09:31:21
What are the principles of a condition variable in synchronization of the processes of operating systems? Alexander Sandler Well, conditional variables allow you to wait for certain condition to occur. In practice your thread may sleep on conditional variable and other thread wakes it up. Conditional variable also usually comes with mutex. This allows you to solve following synchronisation problem: how can you check state of some mutex protected data structure and then wait until it's state changes to something else. I.e. /* in thread 1 */ pthread_mutex_lock(mx); /* protecting state access */

Is it guaranteed that pthread_cond_signal will wake up a waiting thread?

泄露秘密 提交于 2019-11-28 07:10:37
问题 This is a general question. For example, currently two child threads have called pthread_cond_wait(&cond1,&mutex) , and they are both waiting. Then, the parent thread calls pthread_cond_signal(&cond1); pthread_cond_signal(&cond1); Next, my question is, is it guaranteed that both of the two waiting threads will get woken up?(Suppose the first thread woken up releases mutex later at certain stage of execution so that the second thread can acquire it). The reason for me to ask this question is

Using std::mutex, std::condition_variable and std::unique_lock

不羁岁月 提交于 2019-11-27 16:13:02
问题 I'm having some trouble understanding condition variables and their use with mutexes, I hope the community can help me with. Please note, I come from a win32 background, so I'm used with CRITICAL_SECTION, HANDLE, SetEvent, WaitForMultipleObject, etc. Here's my first attempt at concurrency using the c++11 standard library, it's a modified version of a program example found here. #include <condition_variable> #include <mutex> #include <algorithm> #include <thread> #include <queue> #include

What is the difference between busy-wait and polling?

佐手、 提交于 2019-11-27 13:15:16
问题 From the Wikipedia article on Polling Polling, or polled operation, in computer science, refers to actively sampling the status of an external device by a client program as a synchronous activity. Polling is most often used in terms of input/output (I/O), and is also referred to as polled I/O or software driven I/O. Polling is sometimes used synonymously with busy-wait polling (busy waiting). In this situation, when an I/O operation is required the computer does nothing other than check the

Using condition variable in a producer-consumer situation

只愿长相守 提交于 2019-11-27 13:09:17
I'm trying to learn about condition variables and how to use it in a producer-consumer situation. I have a queue where one thread pushes numbers into the queue while another thread popping numbers from the queue. I want to use the condition variable to signal the consuming thread when there is some data placed by the producing thread. The problem is there are times (or most times) that it only pushes up to two items into the queue then hangs. I have indicated in the produce() function where it stops when running in debug mode. Can anyone help me point out why this is happening? I have the

When is a condition variable needed, isn't a mutex enough?

非 Y 不嫁゛ 提交于 2019-11-27 06:23:11
I'm sure mutex isn't enough that's the reason the concept of condition variables exist; but it beats me and I'm not able to convince myself with a concrete scenario when a condition variable is essential. Differences between Conditional variables, Mutexes and Locks question's accepted answer says that a condition variable is a lock with a "signaling" mechanism. It is used when threads need to wait for a resource to become available. A thread can "wait" on a CV and then the resource producer can "signal" the variable, in which case the threads who wait for the CV get notified and can continue