Repository navigation
Threadcall
Home › Threading Support Functions › 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.
Function ThreadCall subname([paramlist]) As Any Ptrsubnameparamlist
Like ThreadCreate, Threadcall creates a thread which runs at the same time as the code calling it. By placing "Threadcall" before almost any normal call to sub, the sub is called inside of a new thread and returns a pointer to that thread.
Using Threadcall is simpler method of creating threads, and allows data to be passed to the thread without global variables or pointers which are not type safe. However, ThreadCreate is more efficient and should be used for programs creating a large number of threads.
While most subroutines are supported, the following types of subroutines may not be called:
When using Threadcall, parenthesis around the parameter list are required unless the subroutine has no parameters.
Warning:
- Presently when Threadcall involves to pass parameters to the thread, there is no guarantee that the corresponding data are still maintained after the end of the Threadcall statement and this until the thread is launched. - That can cause bad behavior:
- see example below where the not displayed parameter
idseems to be the consequence of a prematurely destroyed string argument, - replace
id As Stringwithid As Zstringin the parameters declaration seems to workaround the problem when passing this parameter.
- Therefore it is more advisable for the moment to use ThreadCreate (100% safe) instead.
Threadcall returns an Any Ptr handle to the thread created, or the null pointer (0) on failure.
threadid = ThreadCall subname([paramlist])
- Threading is not allowed in the -lang qb dialect.
-
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, this can't be assumed in Win32. It's an OS, not a FreeBASIC issue.
In Linux, thestdcallandpascalcalling conventions are not supported
In Windows, thepascalcalling convention is not supported.
- New to FreeBASIC
'' Threading using "ThreadCall"
Sub thread( id As String, tlock As Any Ptr, count As Integer )
For i As Integer = 1 To count
MutexLock tlock
Print "thread " & id;
Locate , 20
Print i & "/" & count
MutexUnlock tlock
Next
End Sub
Dim tlock 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!)"Category: Threading Support Functions
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing