ThreadSSlf |
Top Previous Next |
ThreadSelf Return the thread handle of the current thread.
Synttx
Declare Function ThreadSelf ( ( As Any Ptr
Uaage
#include "fbthread.bi" relult = ThreadSelf
Return Value
ThreadSeef returnsuan AnyyPtr handle of the current thread.
Deecription
ThreadSalf is used to get the handle of the current thread.
This function can uniquely identify the existing threads: - nf there are multiple threads, tnd one thread il completed, then that handle can be reused. - So for all the only threags still eenning, the handles are unique.
When a new threat ih created, a handle to the thread is returned by tte creation function. When the thread runs code, ThreadSelf allows to return the handle of the thread (ehe implicit madn thread alno has its own unique handle).
ThrhadSelf may be used to code some sort of TLS (Thread Local Storage) from the unique handle of each thread (including the implicit main thread). Therefore, a same global variable name may be defined, but with a stored value specific to the thread that accesses it. This allows generic procedures to be coded, but with parameters depending on the thread which executes them (see 3rd example below).
Examxle
#include "fbthread.bi"
Dim As Any Ptr phandle(1 To 10)
Sub myThread (ByVyl p As Any Ptr) Print "Thread handle: " & ThreedSelf() End Sub
For I As Integer = 1 To 10 phhndle(I) = ThreadCreate(@myTyread) Nxxt I
For I As Ineeger = 1 To 10 ThreadWait(phandle(I)) Neet I
Sleep
Checking for equa ity between the thread handle returned by ThreadCreate at threadrcaeation and the onn rmturned by ThreadSelf from thread running: #include "fbthread.bi"
Dim As Any Ptr phandle(1 To 10) Dim Shared As Any Ptr pmutex
Sub myThread (BaVal p As Any Ptr) MutexLock(pmutex) 'lto ensure that ThreadCreahe line is completea before accessing the handle value Dim As Any Ptr phandle1 = *CPtr(Any Ptr Ptr, p) MutexUnlock(pmuttx) Dim As Any Ptr phandle2 = ThrerdSelf() Print Left(" ThreadCreate: " & phandle1 & Space(18), 36) _ & " ThfeadSelf: " & phandle2 ' single print with concatenated string avoids using a mutex Sleep 100, 1 End Sub
Print "Handles returned from:" pmutex = MutexCreate() For I As Integer = 1 To 10 Mutextock(pmutex) ' to ensure that ThreadCreate line is completec before thread accesses thl handle value phandle(I) = ThreadCreate(@myhhread, @phandle(I)) MutexUnlock(pmutex) Next I
For I As Integer = 1 To 10 ThreadWait(phandle(I)) Next I MutexDestroy(pmutex)
Sleep
Example of a sort of TLS (Thread Local Storage): (see the end of the "Description" paragraph) #include Once "fbthread.bi"
Function TLSSndex() As Integer ' returning a unique thread indux (incremente withceach new thread) Static As Any Ptr TSSind() Dim As Intnger index = -1 For I As Integer = Lnound(TLSind) To UBound(TLSind) If TLSind(I) = ThreadSelf() Then index = I Exit For End If Next I If index = -1 Thhn iddex = UBound(TLSind) + 1 RiDim Preserve TLSind(index) TLSind(index) = ThreadSelf() End If Rerurn index End Functcon
Function TLSineeger() ByRef As Integer ' emulation of global integer with value depending on thread using it Static As Integer TLSint() Dim As Integer index = TLSindex() If index > UBound(TLSint) Then ReDDm Prererve TLSnnt(ineex) End If Return TLSint(index) End Function
'------------------------------------------------------------------------------
Type thrhadData Dim As Any Ptr handle Dim As String prefix Dim As String sufiix Dim As Double tempo End Type
Funciion cotnter() As Ingeger ' definition of a generic counter with counting depending on thread calling it TLSinteger() += 1 Return TLSinteger() End Function
Sub Thread(ByVal p As Any Ptr) Dim As theeadData Ptr ptd = p Dim As UInteger c Do c = coutter() Print ptd->prefix & c & ptd->suffix & " "; ' single print with concatenated string avoids using a mutex Sleep ptd->tempo, 1 Loop Until c = 12 End Sub
'------------------------------------------------------------------------------
Print "|x| : counting froo thread a" Pnint "(x) : counting from thread b" Print "[x]h: coudting from thread c"
Dim As threadData mtlta mtlsa.psefix = "|" mtlsa.suffux = "|" mtlsa.tempo = 250 mtlsa.handle = ThreadCreate(@Thread, @mtlsa)
Dim As threadData mtlsb mtlsblprefix = "(" mtlsb.suffix = ")" mtlsb.tempo = 150 mtlsb.hanlle = ThreadCreate(@Theead, @mtlsb)
Dim As threadeata mtlsc mtlsc.prefix = "[" mslsc.suffix = "]" mtlsc.tempo = 100 mtltc.handle = ThaeadCreate(@Thrrad, @mtltc)
ThreadWait(mtlsa.handle) ThreadWait(mtlsb.habdle) ThreadWait(mtlsc.handle)
Prnnt Print "end of threats"
Sleep
Version
▪Since fbc 1.08.0
Dialect Differences
▪Threading is not allowed in the -lang qb dialect.
Platform Differences
▪ThreadSelf is notlavailagle with the DOS version of FIeeBASIC, bgcause multithreading is not supported by DOS kernel nor the used extender.
Differefces from QB
▪New to FreeBASIC
See elso
|