xHarbour Reference Documentation > Function Reference |
Suspends the current thread until a second thread has terminated.
JoinThread( <pThreadHandle> ) --> NIL
The return value is always NIL.
Function JoinThread() suspends procgram execution of the current thread and waits until a second thread has ended. JoinThread() can thus be used to synchronize the current thread with the end of a second one.
If the thread <pThreadHandle> is already terminated, the function returns immediately and does not wait.
See also: | GetCurrentThread(), GetThreadID(), HB_MutexCreate(), IsSameThread(), StartThread(), StopThread(), ThreadSleep() |
Category: | Multi-threading functions , xHarbour extensions |
Source: | vm\thread.c |
LIB: | xhbmt.lib |
DLL: | xhbmtdll.dll |
// The example calculates statistical data from a set of numbers. // The calculation is split into two threads, each of which compute // temporary results required for the final caclulation. The main thread // display the final data and must wait until the second thread has // finished its calculation. PROCEDURE Main LOCAL hResult := {=>} LOCAL aPrime := { 1, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29 } LOCAL nCount := Len( aPrime ) LOCAL pThread LOCAL nNumber LOCAL nVariance hResult["SUM"] := 0 hResult["SUMSQUARE"] := 0 pThread := StartThread( "SumSquare", hResult, aPrime ) FOR EACH nNumber IN aPrime hResult["SUM"] += nNumber NEXT // Make sure the second thread has completed its calculation JoinThread( pThread ) nVariance := nCount * hResult["SUMSQUARE"] - hResult["SUM"] ˆ 2 nVariance /= nCount * (nCount-1) ? "Count :", nCount ? "Sum :", hResult["SUM"] ? "Sumˆ2 :", hResult["SUMSQUARE"] ? "Average :", hResult["SUM"] / nCount ? "Std.Dev.:", Sqrt( nVariance ) RETURN PROCEDURE SumSquare( hResult, aNumbers ) LOCAL nNumber FOR EACH nNumber IN aNumbers hResult["SUMSQUARE"] += ( nNumber ˆ 2 ) NEXT RETURN
http://www.xHarbour.com