CONDWAIT
Stops execution of current thread until some condition becomes true
Syntax:
Usage:
Condwait ( handle, mutex )
Parameters:
handle
The handle of a conditional variable.
mutex
The mutex associated with this conditional variable, which must be locked when testing the condition and calling Condwait.
Description:
Function that stops the thread where it is called until some other thread Condsignals or Condbroadcasts the handle.
Once the conditional variable is created with Condcreate and the threads are started, one of more of them (including the implicit main thread executing main program) 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. At the end of the program Conddestroy must be used to avoid leaking resources in the OS.
When calling Condwait, mutex should already be locked (using the same mutex as one used with Condsignal or Condbroadcast). An atomic unlock of the mutex and wait on the conditional variable will occur. The calling thread execution is suspended and does not consume any CPU time until the condition variable is signaled. When the condition variable becomes signaled, mutex will be locked again and then execution will return to the thread after the Condwait call, but with the mutex owned by the caller. The caller is then responsible for unlocking mutex in order to complete Condwait subroutine so that execution after the Condwait call can then resume.
Note: It is a good habit to use Condwait in a protected way against eventual spurious wakeups.
For that, Condwait is put within a loop for checking that a Boolean predicate is indeed true (predicate set true by another thread just before executing Condsignal or Condbroadcast) when the thread has finished waiting:
See example below for detailed coding.
Once the conditional variable is created with Condcreate and the threads are started, one of more of them (including the implicit main thread executing main program) 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. At the end of the program Conddestroy must be used to avoid leaking resources in the OS.
When calling Condwait, mutex should already be locked (using the same mutex as one used with Condsignal or Condbroadcast). An atomic unlock of the mutex and wait on the conditional variable will occur. The calling thread execution is suspended and does not consume any CPU time until the condition variable is signaled. When the condition variable becomes signaled, mutex will be locked again and then execution will return to the thread after the Condwait call, but with the mutex owned by the caller. The caller is then responsible for unlocking mutex in order to complete Condwait subroutine so that execution after the Condwait call can then resume.
Note: It is a good habit to use Condwait in a protected way against eventual spurious wakeups.
For that, Condwait is put within a loop for checking that a Boolean predicate is indeed true (predicate set true by another thread just before executing Condsignal or Condbroadcast) when the thread has finished waiting:
signal-caller:
On the other hand, if the predicate is already true before the thread reaches the loop, Condwait is downright skipped (allowing to take into account a case of Condsignal or Condbroadcast that would have been lost otherwise, because prematurely executed in a second thread before the first thread is really waiting for this).
predicate = true
Condsignal(handle)
waiting-called:Condsignal(handle)
While predicate <> true
predicate = false
the loop can terminate only when the predicate is true.
Condwait(handle, mutex)
Wendpredicate = false
See example below for detailed coding.
Examples:
' This simple example code demonstrates the use of several condition variable routines.
' The main routine creates three threads.
' Two of the threads update a "count" variable.
' The third thread waits until the count variable reaches a specified value.
#define numThread 3
#define countThreshold 6
Dim Shared As Integer count = 0
Dim Shared As Any Ptr countMutex
Dim Shared As Any Ptr countThresholdCV
Dim As Any Ptr threadID(0 To numThread-1)
Dim Shared As Integer ok = 0
Sub threadCount (ByVal p As Any Ptr)
Print "Starting threadCount(): thread#" & p
Do
Print "threadCount(): thread#" & p & ", locking mutex"
MutexLock(countMutex)
count += 1
' Check the value of count and signal waiting thread when condition is reached.
' Note that this occurs while mutex is locked.
If count >= countThreshold Then
If count = countThreshold Then
Print "threadCount(): thread#" & p & ", count = " & count & ", threshold reached, unlocking mutex"
ok = 1
CondSignal(countThresholdCV)
Else
Print "threadCount(): thread#" & p & ", count = " & count & ", threshold exceeded, unlocking mutex"
End If
MutexUnlock(countMutex)
Exit Do
End If
Print "threadCount(): thread#" & p & ", count = " & count & ", unlocking mutex"
MutexUnlock(countMutex)
Sleep 100, 1
Loop
End Sub
Sub threadWatch (ByVal p As Any Ptr)
Print "Starting threadWatch(): thread#" & p & ", locking mutex, waiting for conditional"
MutexLock(countMutex)
' Note that the Condwait routine will automatically and atomically unlock mutex while it waits.
While ok = 0
CondWait(countThresholdCV, countMutex)
Wend
Print "threadWatch(): thread#" & p & ", condition signal received"
Print "threadWatch(): thread#" & p & ", count now = " & count & ", unlocking mutex"
MutexUnlock(countMutex)
End Sub
' Create mutex and condition variable
countMutex = MutexCreate
countThresholdCV = CondCreate
' Create threads
threadID(0) = ThreadCreate(@threadWatch, Cast(Any Ptr, 1))
threadID(1) = ThreadCreate(@threadCount, Cast(Any Ptr, 2))
threadID(2) = ThreadCreate(@threadCount, Cast(Any Ptr, 3))
' Wait for all threads to complete
For I As Integer = 0 To numThread-1
ThreadWait(threadID(I))
Print "Main(): Waited on thread#" & I+1 & " Done"
Next I
MutexDestroy(countMutex)
CondDestroy(countThresholdCV)
' The main routine creates three threads.
' Two of the threads update a "count" variable.
' The third thread waits until the count variable reaches a specified value.
#define numThread 3
#define countThreshold 6
Dim Shared As Integer count = 0
Dim Shared As Any Ptr countMutex
Dim Shared As Any Ptr countThresholdCV
Dim As Any Ptr threadID(0 To numThread-1)
Dim Shared As Integer ok = 0
Sub threadCount (ByVal p As Any Ptr)
Print "Starting threadCount(): thread#" & p
Do
Print "threadCount(): thread#" & p & ", locking mutex"
MutexLock(countMutex)
count += 1
' Check the value of count and signal waiting thread when condition is reached.
' Note that this occurs while mutex is locked.
If count >= countThreshold Then
If count = countThreshold Then
Print "threadCount(): thread#" & p & ", count = " & count & ", threshold reached, unlocking mutex"
ok = 1
CondSignal(countThresholdCV)
Else
Print "threadCount(): thread#" & p & ", count = " & count & ", threshold exceeded, unlocking mutex"
End If
MutexUnlock(countMutex)
Exit Do
End If
Print "threadCount(): thread#" & p & ", count = " & count & ", unlocking mutex"
MutexUnlock(countMutex)
Sleep 100, 1
Loop
End Sub
Sub threadWatch (ByVal p As Any Ptr)
Print "Starting threadWatch(): thread#" & p & ", locking mutex, waiting for conditional"
MutexLock(countMutex)
' Note that the Condwait routine will automatically and atomically unlock mutex while it waits.
While ok = 0
CondWait(countThresholdCV, countMutex)
Wend
Print "threadWatch(): thread#" & p & ", condition signal received"
Print "threadWatch(): thread#" & p & ", count now = " & count & ", unlocking mutex"
MutexUnlock(countMutex)
End Sub
' Create mutex and condition variable
countMutex = MutexCreate
countThresholdCV = CondCreate
' Create threads
threadID(0) = ThreadCreate(@threadWatch, Cast(Any Ptr, 1))
threadID(1) = ThreadCreate(@threadCount, Cast(Any Ptr, 2))
threadID(2) = ThreadCreate(@threadCount, Cast(Any Ptr, 3))
' Wait for all threads to complete
For I As Integer = 0 To numThread-1
ThreadWait(threadID(I))
Print "Main(): Waited on thread#" & I+1 & " Done"
Next I
MutexDestroy(countMutex)
CondDestroy(countThresholdCV)
Platform Differences:
- Condwait is not available with the DOS version / target of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
- In Linux the threads are always started in the order they are created, this can't be assumed in Win32. It's an OS, not a FreeBASIC issue.
Dialect Differences:
- Threading is not allowed in -lang qb
Differences from QB:
- New to FreeBASIC
See also:
Back to Threading Support Functions