Thread Management.
More...
This is provided for compatibility and completeness, we advise you to use std's thread facilities.
SDL offers cross-platform thread management functions. These are mostly concerned with starting threads, setting their priority, and dealing with their termination.
In addition, there is support for Thread Local Storage (data that is unique to each thread, but accessed from a single key).
On platforms without thread support (such as Emscripten when built without pthreads), these functions still exist, but things like ThreadBase.ThreadBase() will report failure without doing anything.
If you're going to work with threads, you almost certainly need to have a good understanding of [CategoryMutex](CategoryMutex) as well.
◆ ThreadCB
- Returns
- a value that can be reported through ThreadBase.Wait().
- Since
- This datatype is available since SDL 3.2.0.
◆ ThreadFunction
- Parameters
-
- Returns
- a value that can be reported through ThreadBase.Wait().
- Since
- This datatype is available since SDL 3.2.0.
◆ ThreadID
These are different from ThreadBase objects, which are generally what an application will operate on, but having a way to uniquely identify a thread can be useful at times.
- Since
- This datatype is available since SDL 3.2.0.
- See also
- ThreadBase.GetID
-
GetCurrentThreadID
◆ ThreadPriority
SDL will make system changes as necessary in order to apply the thread priority. Code which attempts to control thread state related to priority should be aware that calling ThreadBase.SetCurrentPriority may alter such state. SDL_HINT_THREAD_PRIORITY_POLICY can be used to control aspects of this behavior.
- Since
- This enum is available since SDL 3.2.0.
◆ ThreadState
◆ TLSDestructorCallback
This is called when a thread exits, to allow an app to free any resources.
- Parameters
-
value | a pointer previously handed to SetTLS. |
- Since
- This datatype is available since SDL 3.2.0.
- See also
- SetTLS
◆ TLSID
0 is the invalid ID. An app can create these and then set data for these IDs that is unique to each thread.
- Since
- This datatype is available since SDL 3.2.0.
- See also
- GetTLS
-
SetTLS
◆ CleanupTLS()
If you are creating your threads outside of SDL and then calling SDL functions, you should call this function before your thread exits, to properly clean up SDL memory.
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
◆ GetCurrentThreadID()
This thread identifier is as reported by the underlying operating system. If SDL is running on a platform that does not support threads the return value will always be zero.
This function also returns a valid thread ID when called from the main thread.
- Returns
- the ID of the current thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- ThreadBase.GetID
◆ GetTLS()
void * SDL::GetTLS |
( |
TLSID * |
id | ) |
|
|
inline |
- Parameters
-
id | a pointer to the thread local storage ID, may not be nullptr. |
- Returns
- the value associated with the ID for the current thread or nullptr if no value has been set; call GetError() for more information.
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- SetTLS
◆ SetTLS()
If the thread local storage ID is not initialized (the value is 0), a new ID will be created in a thread-safe way, so all calls using a pointer to the same ID will refer to the same local storage.
Note that replacing a value from a previous call to this function on the same thread does not call the previous value's destructor!
destructor
can be nullptr; it is assumed that value
does not need to be cleaned up if so.
- Parameters
-
id | a pointer to the thread local storage ID, may not be nullptr. |
value | the value to associate with the ID for the current thread. |
destructor | a function called when the thread exits, to free the value, may be nullptr. |
- Exceptions
-
- Thread safety:
- It is safe to call this function from any thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetTLS
◆ THREAD_ALIVE
◆ THREAD_DETACHED
◆ THREAD_PRIORITY_HIGH
Initial value:=
SDL_THREAD_PRIORITY_HIGH
◆ THREAD_PRIORITY_NORMAL
Initial value:=
SDL_THREAD_PRIORITY_NORMAL
◆ THREAD_PRIORITY_TIME_CRITICAL
Initial value:=
SDL_THREAD_PRIORITY_TIME_CRITICAL
◆ THREAD_UNKNOWN