condition-variable

How can I implement java-like synchronization (monitors) using the Win32 API?

Each Java object (and its class) has an associated monitor. In pthread terms a Java monitor is equivalent to the combination of a reentrant mutex and a condition variable. For locking, the Win32 API provides Mutex objects (which are reentrant but heavyweight) and Critical Sections (which are non-reentrant but lightweight). It also provi...

What is the consensus number for semaphores?

(I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)? ...

Differences between Conditional variables, Mutexes and Locks

For example the c++0x interfaces I am having a hard time figuring out when to use which of these things (cv, mutex and lock). Can anyone please explain or point to a resource? thanks in regard ...

Guaranteed yielding with pthread_cond_wait and pthread_cond_signal

Assuming I have a C program with 3 POSIX threads, sharing a global variable, mutex, and condition variable, two of which are executing the following psuedocode: ...process data... pthread_mutex_lock( &mutex ); variable = data_ptr; pthread_cond_signal( &cond ); pthread_mutex_unlock( &mutex ); And the third running: while(1) { whil...

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

Hi everyone. 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 ...

Implementing a Priority queue with a Conditional Variable in C

My current understanding of conditional variables is that all blocked (waiting) threads are inserted into a basic FIFO queue, the first item of which is awakened when signal() is called. Is there any way to modify this queue (or create a new structure) to perform as a priority queue instead? I've been thinking about it for a while, but...

Is there an alternative to the threading.Condition variables in python that better support timeouts without polling?

I'm using condition variables in threads that require a timeout. I didn't notice until I saw the CPU usage when having a lot of threads running, that the condition variable provided in the threading module doesn't actually sleep, but polls when a timeout is provided as an argument. Is there an alternative to this that actually sleeps li...

How to connect signals into complex circuits in .NET?

For some concurrent code, I want to connect a bunch of signals together like a circuit. In .NET we can do a WaitAll or WaitAny on a collection of signals. I want to do something like this: WaitAny ( WaitAll (c1, c2), WaitAll (c3, c4) ) ; Unfortunately, the library doesn't support composing these operations into more complex trees. My ...

Using condition variable in a producer-consumer situation

Hello, 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 th...

What are common uses of condition variables in C++?

I'm trying to learn about condition variables. I would like to know what are the common situations where condition variables are used. One example is in a blocking queue, where two threads access the queue - the producer thread pushes an item into the queue, while the consumer thread pops an item from the queue. If the queue is empty, t...

Do condition variables still need a mutex if you're changing the checked value atomically?

Here is the typical way to use a condition variable: // The reader(s) lock(some_mutex); if(protected_by_mutex_var != desired_value) some_condition.wait(some_mutex); unlock(some_mutex); // The writer lock(some_mutex); protected_by_mutex_var = desired_value; unlock(some_mutex); some_condition.notify_all(); But if protected_by_mutex...

Why do condition variables sometimes erroneously wake up?

I've known for eons that the way you use a condition variable is lock while not task_done wait on condition variable unlock Because sometimes condition variables will spontaneously wake. But I've never understood why that's the case. In the past I've read it's expensive to make a condition variable that doesn't have that behavior,...

Why do pthreads’ condition variable functions require a mutex?

I’m reading up on pthread.h; the condition variable related functions (like pthread_cond_wait(3)) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed to do? ...

How can I improve my real-time behavior in multi-threaded app using pthreads and condition variables?

I have a multi-threaded application that is using pthreads. I have a mutex() lock and condition variables(). There are two threads, one thread is producing data for the second thread, a worker, which is trying to process the produced data in a real time fashion such that one chuck is processed as close to the elapsing of a fixed time p...

How to multi-thread this?

I wish to have two threads. The first thread1 occasionally calls the following pseudo function: void waitForThread2() { if (thread2 is not idle) { return; } notifyThread2IamReady(); // i.e. via 1st condition variable Wait for thread2 to finish exclusive access. // i.e. via 2nd condition variable. } The second thread2 is f...

concurrent threads in C programming

I have encountered a problem while implementing wait and signal conditions on multiple threads. A thread needs to lock a mutex and wait on a condition variable until some other thread signals it. In the meanwhile, another thread locks the same mutex and waits on the same condition variable. Now, the thread which is running concurrently ...

Problem waking up multiple threads using condition variable API in win32

Hi everyone, I have a problem in understanding how the winapi condition variables work. On the more specific side, what I want is a couple of threads waiting on some condition. Then I want to use the WakeAllConditionVariable() call to wake up all the threads so that they can do work. Besides the fact that i just want the threads starte...