Example: WaitCond, SignalCond, LockMutex, UnlockMutex
This examples demonstrates creating a thread and putting it to sleep. The main thread will then signal to the thread at a certain point that it wants to reawaken.
This examples demonstrates creating a thread and putting it to sleep. The main thread will then signal to the thread at a certain point that it wants to reawaken.
Global cond_mutex:Int = CreateMutex() Global cond:Int = CreateCond() Global mutex_print:Int = CreateMutex() 'create a thread that points to threadfunc as its runtime Local thread:Int = CreateThread(threadfunc,Null) Local temp:Int 'start our main thread runtime 'this will just wait for teh user to press space and then end Graphics 640,480,0,60 Repeat Cls DrawText("Press space to signal the thread to start",5,5) 'the thread has locked the mutex, but as it is asleep we are still able to... 'lock the mutex successfully temp = TryLockMutex(cond_mutex) DrawText("Locking mutex now = "+temp,5,25) If temp UnlockMutex(cond_mutex) Flip Until KeyHit(KEY_SPACE) EndGraphics 'signal the thread to wakeup SignalCond(cond) 'wait for the thread to end WaitThread(thread) 'close the thread DetachThread(thread) Function threadfunc:Object(nobject:Object) 'this thread will wait (without using up system recources) until ... 'it has been signaled by the main thread To wakeup 'lock the mutex used with the wait condition LockMutex(cond_mutex) 'put this thread to sleep until it recieves a signal, the mutex will be available... 'while the thread is asleep WaitCond(cond,cond_mutex) 'upon wakeup do something interesting tPrint "I am a thread and I have been signaled to start..." For Local index:Int = 1 To 10 tPrint "thread is counting: "+index Next 'release the mutex UnlockMutex(cond_mutex) End Function Function tprint(ntext:String) LockMutex(mutex_print) Print(ntext) UnlockMutex(mutex_print) End Function