Thread Local
 
Declares per-thread storage for a variable

Syntax

[ Private ] Thread Local Dim Shared symbol As DataType [ = constant-expression ]
or
Thread Local Extern symbol [ Alias "external-name" ] As DataType

External C declarations belong inside an Extern...End Extern block.

Description

Each thread that accesses a Thread Local variable gets its own value. A thread's value starts at zero, or at the declared constant initializer. Changes made by one thread do not change the value seen by other threads.

The declared type must be trivially stored and its initializer must be constant. Managed strings, objects with constructors or destructors, dynamic arrays, and dynamic initializers are not supported.

On macOS, native C and LLVM thread-local storage requires macOS 10.7 or later. Apple Silicon targets require macOS 11.0 or later.

Use ThreadCreate to start additional FreeBASIC threads. Without additional threads, a Thread Local variable behaves like a normal shared variable within the current thread.

External C Thread Local imports are supported by the native GAS backends for Linux executables. GAS backends diagnose these imports for other targets and dynamic libraries, where the platform TLS relocation model is different.

Example

Thread Local Dim Shared currentRequest As Integer = 0

Sub HandleRequest(ByVal requestNumber As Integer)
    currentRequest = requestNumber
    Print currentRequest
End Sub

HandleRequest(1)


Dialect Differences

  • Not available in the -lang qb dialect.

See also