Creates a conditional variable to be used in synchronizing threads
Syntax
Usage
result = CondCreate
Return Value
A handle to a newly created conditional variable, or the null pointer (0) on failure.
Description
Once the conditional is
Condcreated and the threads are started, one or more of them can be set to
CondWait for the conditional, they will be stopped until some other thread
CondSignals that the waiting thread can restart.
CondBroadcast can be used to restart all threads waiting for the conditional (including the main thread executing main program). At the end of the program
CondDestroy must be used to avoid leaking resources in the OS.
Example
''
'' make newly-created threads wait until all threads are ready, then start them all at once
''
Dim Shared hcondstart As Any Ptr
Dim Shared hmutexstart As Any Ptr
Dim Shared start As Integer = 0
Dim Shared threadcount As Integer
Dim Shared hmutexready As Any Ptr
Dim Shared hcondready As Any Ptr
Sub mythread(ByVal id_ptr As Any Ptr)
Dim id As Integer = Cast(Integer, id_ptr)
Print "Thread #" & id & " is waiting..."
'' signal that this thread is ready
MutexLock hmutexready
threadcount += 1
CondSignal hcondready
MutexUnlock hmutexready
'' wait for the start signal
MutexLock hmutexstart
Do While start = 0
CondWait hcondstart, hmutexstart
Loop
'' now this thread holds the lock on hmutexstart
MutexUnlock hmutexstart
'' print out the number of this thread
For i As Integer = 1 To 40
Print id;
Next i
End Sub
Dim threads(1 To 9) As Any Ptr
hcondstart = CondCreate()
hmutexstart = MutexCreate()
hcondready = CondCreate()
hmutexready = MutexCreate()
threadcount = 0
For i As Integer = 1 To 9
threads(i) = ThreadCreate(@mythread, Cast(Any Ptr, i))
If threads(i) = 0 Then
Print "unable to create thread"
End If
Next i
Print "Waiting until all threads are ready..."
MutexLock(hmutexready)
Do Until threadcount = 9
CondWait(hcondready, hmutexready)
Loop
MutexUnlock(hmutexready)
Print "Go!"
MutexLock hmutexstart
start = 1
CondBroadcast hcondstart
MutexUnlock hmutexstart
'' wait for all threads to complete
For i As Integer = 1 To 9
If threads(i) <> 0 Then
ThreadWait threads(i)
End If
Next i
MutexDestroy hmutexready
CondDestroy hcondready
MutexDestroy hmutexstart
CondDestroy hcondstart
Platform Differences
- Condcreate is not available with the DOS version / target of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
Dialect Differences
Differences from QB
See also