[Wish] Condition with wait(), signal(), signalAll()
Posted: Fri Apr 23, 2010 7:40 am
Hi,
I have the wish for conditions to extent the mutex system. It should be similar to pthread_mutex_t and pthread_cond_t from the c++ header 'pthread.h'.
You can find the complete description here and here.
Up to now we have the functions 'LockMutex()' and 'UnlockMutex()' to synchronize threads. But without conditions we can get trouble with implementing the single-writer-multiple-reader-problem. I thought the following functions were useful:
Before you call ConditionWait(hCondition, hMutex), you have to lock the mutex you want to use. ConditionWait(...) unlocks the mutex and blocks the current thread without a busy wait to save cpu cycles. So an other thread can get the lock, calls ConditionSignal(hCondition) and unlocks the lock. At least one of the waiting threads instantly get this signal and try to lock again. If it gets the lock, it returns from ConditionWait().
You can use conditions for barriers too.
By the way: What's about volatile variables like in C(++)?
I have the wish for conditions to extent the mutex system. It should be similar to pthread_mutex_t and pthread_cond_t from the c++ header 'pthread.h'.
You can find the complete description here and here.
Up to now we have the functions 'LockMutex()' and 'UnlockMutex()' to synchronize threads. But without conditions we can get trouble with implementing the single-writer-multiple-reader-problem. I thought the following functions were useful:
Code: Select all
hCondition.i = CreateCondition() ;creates a new condition
hMutex.i = CreateMutex()
ConditionWait(hCondition, hMutex) ;release the lock and wait for a signal
ConditionSignal(hCondition) ;signals at least one waiting thread. If no thread is waiting: nop
ConditionSignalAll(hCondition); signals all waiting threads.
You can use conditions for barriers too.
By the way: What's about volatile variables like in C(++)?
