-
Notifications
You must be signed in to change notification settings - Fork 13
Threads
- Overview
- Creating a Thread
- Destroying a Thread?
- Waiting/Joining for Threads to Exit
- Terminate a thread
- Query the Thread State
- Notes
In this section, you will learn the Basics about creating and handling of Threads.
With Threads, you can run multiple pieces of code in parallel.
Threads require the use of Synchronization methods to prevent race conditions.
Call fplThreadCreate() with a pointer to callback and a user pointer argument, to create and immediately run a Thread.
void MyThreadProc(const *context, void *data) {
// ... do something here
}
void RunMyThread() {
int myData = 42;
*thread = (MyThreadProc, &myData);
(thread, );
}Note: The internal Thread resources will be cleaned up automatically after your code has finished running.
Warning: When a Thread has finished running, you cannot use the same
fplThreadHandle anymore -> It may be reassigned to another Thread in the future.
You don't have to manually release the Thread resources, this will be cleaned up automatically when either the Thread ends naturally or when it was terminated forcefully using fplThreadTerminate() .
Warning: Do not call
fplThreadTerminate() to stop or release a Thread! Let the thread exit naturally.
Call fplThreadWaitForOne() with a pointer to fplThreadHandle and a fplTimeoutValue argument, to wait for a single Thread to finish.
// Wait until the thread is finished
(thread, );
// ... or
// Wait at max for 5 seconds to finish the thread
(thread, 5000);Call fplThreadWaitForAny() to wait until at least for one Thread to finish.
Example, default thread handle array:
**threads; // Thread handle array already initialized
// Wait until one of the threads is finished
(threads, numOfThreads, sizeof( *), );
// ... or
// Wait at max for 5 seconds to finish at least one of the threads -> default stride
(threads, numOfThreads, sizeof( *), 5000);Example, custom struct with stored thread handle:
typedef struct MyCustomStruct {
int a;
void *ptr;
*thread;
short b;
} MyCustomStruct;
MyCustomStruct *customThreads; // Struct array already initialized
**firstPointerToThread = &customThreads[0].thread;
// Wait until one of the threads is finished
(firstPointerToThread, numOfThreads, sizeof(MyCustomStruct), );
// ... or
// Wait at max for 5 seconds to finish at least one of the threads -> default stride
(firstPointerToThread, numOfThreads, sizeof(MyCustomStruct), 5000);Call fplThreadWaitForAll() to wait for all Threads to be finished.
Example, default thread handle array:
**threads; // Thread handle array already initialized
// Wait until all of the threads are finished
(threads, numOfThreads, sizeof( *), );
// ... or
// Wait at max for 5 seconds to finish all threads
(threads, numOfThreads, sizeof( *), 5000);Example, custom struct with stored thread handle:
typedef struct MyCustomStruct {
int a;
void *ptr;
*thread;
short b;
} MyCustomStruct;
MyCustomStruct *customThreads; // Struct array already initialized
**firstPointerToThread = &customThreads[0].thread;
// Wait until all of the threads are finished
(threads, numOfThreads, sizeof(MyCustomStruct), );
// ... or
// Wait at max for 5 seconds to finish all threads
(threads, numOfThreads, sizeof(MyCustomStruct), 5000);Call fplThreadTerminate() with a pointer to fplThreadHandle as an argument, to forcefully terminate a thread.
Note: Using this will almost immediately terminate the thread and releases its resources.
It is safe to call this when a thread is already terminated.
Call fplGetThreadState() with a pointer to fplThreadHandle as an argument, to query the current state.
If a thread is stopped or in the process of getting stopped, it will return fplThreadState_Stopped or fplThreadState_Stopping respectively.
If a thread is started or in the process of getting started, it will return fplThreadState_Running or fplThreadState_Starting respectively.
Note: Your code should always ensure that it will exit eventually. You can use
Synchronization methods to achieve this.
It is bad practice to "Terminate" a thread, you should design your code to let threads end naturally.
- Assertion & Debug
- Atomic operations
- Audio functions
- Clipboard functions
- Console functions
- Constants
- Display/Monitor functions
- Dynamic library loading
- Error Handling
- Files/IO functions
- Function macros
- Hardware Infos
- Input types and functions
- Localization functions
- Logging
- Memory Macros
- Memory functions
- Operating system Infos
- Path functions
- Platform functions
- Session Infos
- Settings & Configurations
- Storage class identifiers
- String functions
- Threading and synchronizations routines
- Timing functions
- Video functions
- Window events
- Window functions
- fplARMCPUCapabilities
- fplAudioChannelMap
- fplAudioDeviceID
- fplAudioDeviceInfo
- fplAudioFormat
- fplAudioSettings
- fplColor32
- fplConditionVariable
- fplConsoleSettings
- fplCPUCapabilities
- fplCPUIDLeaf
- fplDateTime
- fplDateTimeCreationResult
- fplDateTimeResult
- fplDisplayInfo
- fplDisplayMode
- fplDynamicLibraryHandle
- fplEndianess
- fplEvent
- fplFileEntry
- fplFileHandle
- fplFilePermissions
- fplFileTimeStamps
- fplGamepadButton
- fplGamepadData
- fplGamepadEvent
- fplGamepadInfo
- fplGamepadInputBinding
- fplGamepadMapping
- fplGamepadSettings
- fplGamepadState
- fplGamepadStates
- fplGraphicsApiSettings
- fplImageSource
- fplInputBackendMask
- fplInputBackendSupport
- fplInputDevice
- fplInputDeviceGuid
- fplInputSettings
- fplInternalConditionVariable
- fplInternalDynamicLibraryHandle
- fplInternalFileEntryHandle
- fplInternalFileHandle
- fplInternalFileRootInfo
- fplInternalMutexHandle
- fplInternalSemaphoreHandle
- fplInternalSignalHandle
- fplInternalThreadHandle
- fplKeyboardEvent
- fplKeyboardState
- fplLogSettings
- fplLogWriter
- fplLogWriterConsole
- fplLogWriterCustom
- fplMemoryAllocationSettings
- fplMemoryBlock
- fplMemoryInfos
- fplMemorySettings
- fplMouseEvent
- fplMouseState
- fplMutexHandle
- fplOpenGLSettings
- fplOSVersionInfos
- fplSemaphoreHandle
- fplSettings
- fplSignalHandle
- fplSpecificAudioSettings
- fplThreadHandle
- fplThreadParameters
- fplTimestamp
- fplVersionInfo
- fplVideoBackBuffer
- fplVideoRect
- fplVideoRequirements
- fplVideoRequirementsVulkan
- fplVideoSettings
- fplVideoSurface
- fplVideoSurfaceOpenGL
- fplVideoSurfaceVulkan
- fplVideoWindow
- fplVulkanSettings
- fplWindowCallbacks
- fplWindowDropFiles
- fplWindowEvent
- fplWindowPosition
- fplWindowSettings
- fplWindowSize
- fplX86CPUCapabilities