Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
135 views

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 ...
Joe J's user avatar
  • 1,391
4 votes
3 answers
237 views

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) {...
FlameWare's user avatar
1 vote
0 answers
83 views

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 ...
Utkarsh Munjal's user avatar
1 vote
1 answer
51 views

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; ...
user29207775's user avatar
2 votes
3 answers
129 views

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 ...
user3191398's user avatar
0 votes
0 answers
80 views

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, ...
sleepyhead's user avatar
2 votes
1 answer
134 views

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 ...
user19800684's user avatar
0 votes
0 answers
110 views

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 ...
牛牪犇's user avatar
0 votes
1 answer
127 views

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 ...
Seektech's user avatar
0 votes
1 answer
112 views

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 ...
Zebrafish's user avatar
  • 16.5k
1 vote
1 answer
149 views

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> #...
yongjay's user avatar
  • 11
1 vote
1 answer
73 views

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 ...
Ethouris's user avatar
  • 1,921
1 vote
1 answer
153 views

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::...
jpo38's user avatar
  • 21.9k
1 vote
1 answer
171 views

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::...
lustfully's user avatar
0 votes
0 answers
71 views

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<...
Mathai's user avatar
  • 839
0 votes
0 answers
85 views

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 ...
Makise Kurisu's user avatar
0 votes
1 answer
141 views

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 ...
douyu's user avatar
  • 2,639
-1 votes
3 answers
243 views

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::...
DarkRise's user avatar
1 vote
1 answer
149 views

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 ...
neuron mac's user avatar
0 votes
1 answer
123 views

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 ...
soa008's user avatar
  • 3
0 votes
1 answer
112 views

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 ...
Arun C's user avatar
  • 1
0 votes
1 answer
276 views

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 ...
SpeakX's user avatar
  • 427
0 votes
1 answer
105 views

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 ...
wwite's user avatar
  • 13
1 vote
1 answer
290 views

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 <...
NeverStopLearning's user avatar
0 votes
2 answers
222 views

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::...
Vroomfondel's user avatar
  • 2,928
0 votes
1 answer
116 views

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(...
Vroomfondel's user avatar
  • 2,928
-1 votes
2 answers
136 views

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 ...
Morgan's user avatar
  • 354
3 votes
3 answers
749 views

On the reference documentation for std::condition_variable there's this example: #include <condition_variable> #include <iostream> #include <mutex> #include <string> #include &...
Tyler's user avatar
  • 53
0 votes
1 answer
112 views

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); ...
Lorenzo's user avatar
6 votes
1 answer
1k views

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 ...
Jan Schultke's user avatar
  • 44.4k
0 votes
1 answer
318 views

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 ...
Burak's user avatar
  • 21
0 votes
1 answer
228 views

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,...
user1584421's user avatar
  • 3,933
0 votes
0 answers
74 views

#include <mutex> #include <queue> #include <thread> #include <vector> #include <iostream> template <typename T> class ThreadSafeQueue { private: std::queue<T&...
Jiashan Wang's user avatar
0 votes
2 answers
86 views

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::...
f1msch's user avatar
  • 665
-1 votes
1 answer
192 views

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 ...
Intangible _pg18's user avatar
1 vote
1 answer
264 views

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> #...
york king's user avatar
0 votes
1 answer
186 views

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 ...
Samuel Afon's user avatar
1 vote
0 answers
58 views

I've the following code: #include <mutex> #include <condition_variable> #include <thread> #include <iostream> #include <vector> #include <syncstream> int main() { ...
Jepessen's user avatar
  • 12.7k
0 votes
1 answer
299 views

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:...
Jepessen's user avatar
  • 12.7k
0 votes
1 answer
125 views

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): ...
Sultan Ahmed's user avatar
  • 2,230
2 votes
1 answer
125 views

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::...
Amir Kirsh's user avatar
  • 14.5k
1 vote
1 answer
956 views

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 ...
Paul's user avatar
  • 93
1 vote
0 answers
142 views

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 ...
lior.i's user avatar
  • 795
1 vote
2 answers
228 views

#include <thread> #include <mutex> #include <condition_variable> #include <iostream> #include <chrono> using namespace std; using namespace std::chrono_literals; ...
zhzhy's user avatar
  • 25
2 votes
1 answer
285 views

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 ...
jelly's user avatar
  • 23
1 vote
1 answer
78 views

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; ...
White Owl's user avatar
  • 1,524
0 votes
1 answer
139 views

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 ...
KWL's user avatar
  • 1
2 votes
1 answer
148 views

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::...
Agritite's user avatar
1 vote
1 answer
3k views

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 ...
Zlatan Radovanovic's user avatar
2 votes
2 answers
461 views

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 <...
SiennaRose's user avatar

1
2 3 4 5
16