[Wish] Condition with wait(), signal(), signalAll()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

[Wish] Condition with wait(), signal(), signalAll()

Post by NicTheQuick »

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:

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.
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(++)? :wink:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: [Wish] Condition with wait(), signal(), signalAll()

Post by ts-soft »

What is the difference to Semaphore in PB?
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [Wish] Condition with wait(), signal(), signalAll()

Post by NicTheQuick »

If there is no condition waiting the ConditionSignal() will become a NOP (no operation). If you have a Semaphore and call SignalSemaphore(), then the internal counter always will be increased by 1, even if there is no waiting semaphore. This results in a problem, if an other thread is calling WaitSemaphore() and the internal counter is greater than 0. Because in this case WaitSemaphore() will not waiting for the next signal. A condition will always wait until you call ConditionSignal() or ConditionSignalAll() after.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [Wish] Condition with wait(), signal(), signalAll()

Post by NicTheQuick »

*push*

This thing is really necessary for proper thread handling.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: [Wish] Condition with wait(), signal(), signalAll()

Post by idle »

Could be useful it should be easy enough to implement in a macro with c backend I assume p thread is included on windows and mac
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: [Wish] Condition with wait(), signal(), signalAll()

Post by NicTheQuick »

I already inplemented this on Linux with pthreads but I wish it was native in Purebasic.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply