753 questions
1
vote
1
answer
128
views
Correct way to set ready flag for std::condition_variable [duplicate]
I have two threads where one thread needs to wait for the other thread to complete its task. To solve this problem I decided to use std::condition_variable.
My question is, should the thread that is ...
4
votes
3
answers
236
views
Why do we need condition variables when we can use two semaphores?
An example of producer and consumer threads is usually given.
But this can be done with two semaphores.
Why do we need condition variables then?
Example with pthread library:
// write thread
while(1)
{...
1
vote
0
answers
82
views
Managing Worker Thread Wake-Up in C++: Separate vs. Shared Condition Variables?
I have a queue of worker thread IDs in C++, and a coordinator thread wakes each thread in order. The woken thread then interacts with the coordinator thread to perform some work. Once done, the ...
1
vote
1
answer
50
views
Synchronization using condition_variable
There is such an interesting little code (I'll take the most important thing, since my project is quite large
void RunForever(int time_out)
{
int criticalSize = 3;
...
2
votes
3
answers
129
views
Why does using separate lock_guard for cv wait and pop() cause a segmentation fault?
I created a Packets class to synchronize data processing between consumers and producers. My design uses separate locks for the condition variable (cv wait) and the operation of popping packets from ...
0
votes
0
answers
79
views
Why is my Timer class not executing when started consecutively with no delay?
I have a custom Timer class in C++ that uses std::async to run a timer in a separate thread. The problem I'm facing is that when I call start() on the same Timer object consecutively with no delay, ...
2
votes
1
answer
132
views
(c++ thread and condition_variable) Could this program never end forever?
I think the source code below may not end forever.
Assume that all waiter threads have entered the wait state.
After that, waker call notify_all()
One thread, t1, wakes up and does something. And ...
0
votes
0
answers
110
views
Why is the condition_variable in the thread pool not woken up?
I wrote a thread pool by myself, and the Result class is used to obtain the calculation results of the thread pool tasks. Initially, I used a simple semaphore mechanism to implement thread ...
0
votes
1
answer
123
views
C++ condition variable doesn't block the thread on wait_until() using the time_poin::max()?
The below code doesn't block the thread and it gets into an infinite loop and keep printing
Waiting...
Waiting...
My idea is, the thread should block on the max() time and when it's set to now(), it ...
0
votes
1
answer
108
views
How to prevent worker threads from idling until next condition variable notify
Let's just say I have a bunch of worker threads waiting until there are tasks in the queue.
Generally you have the producer call the produce function, and then notify_one so that the worker thread can ...
1
vote
1
answer
149
views
Why is the output of this code incorrect?
Normally, the output should always be 200.But the output of "totalMoney" sometimes a number less than 200. What is the issue here and how to correct it?
#include <condition_variable>
#...
1
vote
1
answer
69
views
What is the return value of `wait_until` in case of triggered or spurious wakeup, when timeout was reached?
I have a question for the "first" version of std::condition_variable::wait_until, that is, the one without the condition predicate parameter, which should return std::cv_status value.
The CV ...
1
vote
1
answer
144
views
How to correctly use condition_variable and time_wait/timed_join in threads?
I'm experiencing a wierd behaviour with thread. I isolated the problem in this simple example:
#include <iostream>
#include <boost/thread.hpp>
#define SLEEP_DURATION 1000
static boost::...
1
vote
1
answer
171
views
Question about condition_variable, why condition_variable is paired with mutex [duplicate]
I have been learning std::condition_variable recently and there is just some question that I cannot understand.
Cppreference and many other tutorials give example like this:
std::mutex m;
std::...
0
votes
0
answers
70
views
Producer-consumer problem with queue and semaphore
In Stroustrup's book The C++ Programming Language, there is a code example that demonstrates the use of condition variables:
class Message { // object to be communicated
//...
};
queue<...