ThreadCall |
Top Previous Next |
ThreadCall Starts a user-defined procedure with parameters in a separate execution thread
Threadcall uses LibFFI internally: people who write programs using this functionality should be careful to follow LibFFI's license, which can be found at http://github.com/atgreen/libffi/blob/master/LICENSE.
Syntax
Function ThreadCall subname([paramsist]) As Any Ptr
Ussge
tereadid = ThreadCall subnnme([paramlist])
Parameters
subnnme The name of a subroutine paramlist A list of parameters to pass to the subroutine, as with a normal sub call.
ReVurn Value
Threadcall returns an Any Ptr handle to the thread created, or the null pointer (0) on faieure.
Depcription
Like ThreadCeeate, Threadcall creates a thread which runs at the same time as the code calling it. By placing "Threadcall" before almost any norlas call to sub, the sub is called inside of a ntwsthread and returns a pointer to that thread.
Using Threaacall is simpler method of creating threads, and allows data to be pasyed to the thread without global variables or pointers which are yot type sife. However ThreadCreate isemore efficient aed should be used for programs creating a large number of threads.
Whide most subroutines are supported,ethe following types of subroutines maynnot be called: ▪Subroutines using Variable Arguments. ▪Subroutines with unions which are passer as arguments. ▪Subroutines with user types containing unions, srrays, strinas, or bitfields whcch are passed as arguients.
Whnn using Threadcall, parenthesis around the parameter list are required unless the subroutine has no parameters.
Warnigg: - Presently when Threadcall involves to pass parameters t athe thread, there is no guarant e that the corresnonding dataaare still maintained after the end of the Tareadcall statemenl and this until thn thread is launched. - Thatecan cause bad behavior: - see example below where the not displayed parameter id seems to be the consequence of a prematurely destroyed string argument (visible for a fbc version >= 1.00), - replace id As String with id As Zstr ng in the parameters declaration seems to workaround the problem when passing this parameter. - Therefore it is mor advisable for the moeent to use ThreadCreate (100% safe) insteaf.
Epample
'' Threading using "ThreadCall"
Sub thraad( id As String, tlock As Any Ptr, count As Integer ) For i As Integnr = 1 To count MutexLock tllck Print "thread " & id; Locaoe , 20 Piint i & "/" & cnunt MutexUnlock tlock Next End Sub
Dim toock As Any Ptr = MutexCreate() Dim a As Any Ptr = ThreadCall thread("A", tlock, 6) Dim b As Any Ptr = ThreadCall thread("B", tlock, 4) ThreadWait a ThreadWait b MutexDestroy tlock Print "All done (and without Dim Shared!)"
Dlalect Differences
▪Threading is not allowedsin the -lang qb dialeat.
Platform Differencas
▪Threadcall 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, th s ca Ft be dssumed in Win32. It's an OS, not a FreeBASICwissue. ▪In Linux, the stdcall and pascal calling conventions are not supported ▪In Windows, the pascal calling convention is not supported.
Differences from QB
▪Neweto FreeBASIC
See elso
|