753 questions
1
vote
1
answer
135
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
237
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
83
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
51
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
80
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
134
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
127
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
112
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
73
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
153
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
71
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<...
0
votes
0
answers
85
views
Why does my producer-consumer program using pthread_cond_signal get stuck?
I am writing a producer-consumer program using pthread in C. The program should produce and consume items up to a limit n. However, my program usually gets stuck quickly when n = 1 and n = 2.
To be ...
0
votes
1
answer
141
views
Why can std::conditional_variable only be used with std::unique_lock? [duplicate]
std::condition_variable works only with std::unique_lock<std::mutex>, which allows for maximal efficiency on some platforms. std::condition_variable_any provides a condition variable that works ...
-1
votes
3
answers
243
views
Why does the condition_variable destructor hang this program?
I have a program, which I distilled into this short MRE:
#include <mutex>
#include <condition_variable>
#include <thread>
#include <unistd.h>
std::mutex mu;
std::...
1
vote
1
answer
149
views
In C++, could a thread waiting for conditional variable notify itself?
I have a sender() function, which sends out UDP packet. At the end of the sender() it wakes up a receiver thread to wait for the UDP response with a timeout.
Here the sender() could be called by main ...
0
votes
1
answer
123
views
Issue with synchronization of threads using condition variables to output the Mandelbrot
I try to syncronize threads with condition variables to output mandelbrot but i get wrong mandelbort.
The functions output_mandel_line and compute_mandel_line are given and are correct. I made the
...
0
votes
1
answer
112
views
Multi-Threaded Code only runs sequentially
I've written a simple Class that will execute upto N blocking tasks (N specified during initialization) concurrently using unique_lock & condition_variable.
My code compiles and runs but only ...
0
votes
1
answer
276
views
Using std::condition_variable with custom (spin) mutex?
I'm encountering difficulties using std::condition_variable with my custom spin mutex implementation. It seems that std::condition_variable expects a std::mutex to be associated with its lock, leading ...
0
votes
1
answer
105
views
No progress in a two thread C++ program to print odd and even elements of a vector
I was asked to implement a program during an interview which spawns 2 threads taking a given input vector, one thread is responsible for only printing the even elements of the vector, and the other ...
1
vote
1
answer
290
views
Why does version 3.22.0 of Valgrind-based Helgrind thread error detector reports data races and version 3.18.1 does not?
1.Background
The following C++ code from cppreference.com illustrates how std::condition_variable is used in combination with a std::mutex to facilitate inter-thread communication.
#include <...
0
votes
2
answers
222
views
Is `std::atomic<>` necessary or overkill for `std::condition_variable`?
Suppose there is a simple buffer which serves as the transfer point for some data between a producer and consumer thread. From reading around I have put together the following:
const std::chrono::...
0
votes
1
answer
116
views
What does `std::move` on a predicate lambda mean?
I am reading through https://en.cppreference.com/w/cpp/thread/condition_variable/wait_for and there is the line:
return wait_until(lock, std::chrono::steady_clock::now() + rel_time, std::move(...
-1
votes
2
answers
136
views
Most efficient way to signal consumer thread from multiple producer threads using condition variables
I have N work threads that does a calculation that one other consumer thread is waiting for.
I am doing this with a condition_variable (CV) and an atomic counter that starts at N, each worker ...
3
votes
3
answers
749
views
C++ condition_variable why is lock required?
On the reference documentation for std::condition_variable there's this example:
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include &...
0
votes
1
answer
112
views
Semaphores waiting for child threads
I was reading some code for an exam, and it is something like:
sem_t s;
int main(int argc ,char *argv[]) {
thread_t p1, p2, p3;
sem_init(&s,X,X)
thread_create(&p1,child);
...
6
votes
1
answer
1k
views
std::atomic<bool>::wait vs. std::condition_variable::wait
I have a single producer, single consumer use case where the consumer blocks until the producer has made new data available.
Here are two synchronization approaches that implement this:
New C++20 ...
0
votes
1
answer
318
views
Why using while control instead of if control in condition variables to synchronize different threads?
I have simple producer, consumer problem. While producer thread adding 10$ to global money variable, consumer thread will spend money. But there is one condition that money cannot be under 0. The ...
0
votes
1
answer
228
views
Replace boost::timed_wait() with std in c++17
I have a condition variable std::condition_variable my_cond;
I want to be able to replace boost::timed_wait() with an std equivalent one.
If the previously mentioned condition variable was a boost one,...
0
votes
0
answers
74
views
condition variable wait blocks thread join
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <iostream>
template <typename T>
class ThreadSafeQueue {
private:
std::queue<T&...
0
votes
2
answers
86
views
Could two condition_variable.wait_for used with a single lock?
If codes like
std::mutex mut;
std::condition_variable cond;
bool is_ok = false;
void func() {
std::unique_lock<std::mutex> lck(mut);
if (!cond.wait_for(lck,
std::...
-1
votes
1
answer
192
views
Condition Variable to solve the Multi Producer and Multi Consumer problem
I am currently studying Operating System and I came across "Solving concurrency problems using Condition Variables".
The task is to Simulate a Multi-Threaded Web Server with a Queue of size ...
1
vote
1
answer
264
views
Use C++11 condition_varible to make 3 threads print 1-100 in turns. But the dead lock confuses me
I want to write a program use 3 threads to print 1-100 in turns using C++11 condition_variable, but dead lock confused me, here are my code:
the tmp.cpp is:
#include <condition_variable>
#...
0
votes
1
answer
186
views
What is the proper way to use conditional variables?
My assignment provides running code that uses a high percentage of the CPU when running. The goal is to reduce that amount by implementing conditional variables in the producer consumer problem.
I ...
1
vote
0
answers
58
views
Notify when all thread loops ended their iteration in main thread
I've the following code:
#include <mutex>
#include <condition_variable>
#include <thread>
#include <iostream>
#include <vector>
#include <syncstream>
int main() {
...
0
votes
1
answer
299
views
Error when exiting a program using `std::condition_variable`
I've a problem with some code with the same logic of the following one:
#include <mutex>
#include <condition_variable>
#include <thread>
#include <iostream>
int main() {
std:...
0
votes
1
answer
125
views
make model_averaging exclusive in federated learning using Python Threads
I am creating num_of_clients threads using the following code:
sockets_thread = []
no_of_client = 1
all_data = b""
while True:
try:
for i in range(no_of_client):
...
2
votes
1
answer
125
views
Setting condition variable monitor flag without a lock, is it valid?
The following code uses condition variable and a monitor flag to synchronize an operation between the main thread and thread2:
int main() {
std::mutex m;
std::condition_variable cv;
std::...
1
vote
1
answer
956
views
Condition_variable wait_for timeout issue
I am trying to learn how to get conditional variables working with some threads, but am having some troubles. The goal is to get the main thread to spawn some 'tasks' for a set period of time which ...
1
vote
0
answers
142
views
Boost IPC condition variable blocks after one process crashes and restart
I have an interprocess condition variable to sync my processes, there is one notifier that notify and two waiters that timed_wait for notification or timeout.
If one of the waiters crashes and ...
1
vote
2
answers
228
views
How do condition variable and mutex work in C++?
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono_literals;
...
2
votes
1
answer
285
views
Should I unlock first before broadcasting
I'm confused about the behaviours of condition variables in Go.
In the main goroutine, I acquire the lock and call Cond.Wait() in a for loop checking the shared memory. In the working goroutine, I ...
1
vote
1
answer
78
views
If pthread_cond_wait() requires locked mutex, doesn't it block the thread which changed condition?
I need a two-threaded application. One thread prepare data, one process it. If there is no data to process, the second thread should sleep. Doing as tons of tutorials suggests:
pthread_mutex_t mtx;
...
0
votes
1
answer
139
views
Is there a way to force std::condition_variable::notify_one in c++ to notify certain thread?
I have multiple threads, created constantly in a loop, that access and modify single variable protected by mutex and synchronized by single condition_variable. While it works fine is there a way to ...
2
votes
1
answer
148
views
Is the "unique_lock<mutex>& lock" parameter really unavoidable when using condition_variable with atomic_flag, or am I missing something?
What I have here is an std::jthread to run some tasks every 10 seconds,
while allowing the thread to quit on signal during sleep, which would probably be something like this:
void ThreadMain(std::...
1
vote
1
answer
3k
views
Single producer multiple consumers C++
I am trying to implement a program that consists of a producer thread adding objects to a std::vector and multiple consumer threads removing objects from the same vector until it's empty. I am using a ...
2
votes
2
answers
461
views
Is there a way to use std::condition_variable::wait(lock, pred) without a lambda?
I found this code online explaining how to use a std::condition_variable to solve the Producer-Consumer Problem: Producer-Consumer Problem Using Condition Variable in C++
#include <...