xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

JoinThread()

Suspends the current thread until a second thread has terminated.

Syntax

JoinThread( <pThreadHandle> ) --> NIL

Arguments

<pThreadHandle>
This is the handle of the thread to wait for. A thread handle is returned from function StartThread().

Return

The return value is always NIL.

Description

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.

Info

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

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe