Threads
Compute-intensive tasks can be offloaded from the main emulation flow with threads. Unless otherwise stated, all structures, functions and constants in this page are provided by 86box/plat.h
.
Warning
86Box API functions (excluding those in this page) are generally not thread-safe and must be called from the main emulation thread. Thread-unsafe actions (such as raising an interrupt) can be performed by the callback of a free-running timer which looks for data written to the device’s state structure by a thread, as timers run on the main emulation thread.
Note
The contents of thread_t
and other structures used by thread_*
functions are platform-specific; therefore, pointers to those structures should be treated as opaque pointers.
Starting
Threads can be started with the thread_create
function. Additionally, the thread_wait
function can be used to wait for a thread’s function to return.
Parameter |
Description |
---|---|
|
Function to run in the thread. Takes the form of:
|
|
Opaque pointer passed to the |
Return value |
|
Parameter |
Description |
---|---|
|
|
Return value |
|
Events
Events allow for synchronization between threads. An event, represented by an event_t
pointer returned by the thread_create_event
function, can be set (thread_set_event
function) or reset (thread_reset_event
function), and a thread can wait for an event to be set with the thread_wait_event
function. Events that are no longer to be used should be deallocated with the thread_destroy_event
function.
Parameter |
Description |
---|---|
Return value |
|
Parameter |
Description |
---|---|
|
|
Parameter |
Description |
---|---|
|
|
|
Maximum amount of time in milliseconds (not microseconds, unlike timers) to spend waiting for this event to be set. If set to |
Return value |
|
Note
A thread_wait_event
call does not reset the event once it is set; the event must be reset manually with thread_reset_event
. thread_wait_event
returns immediately if the event is already set.
Mutexes
Mutexes, also known as locks, can control access to a shared resource, ensuring no concurrent modifications or other issues arise from multiple threads attempting to use the same resource at the same time. A mutex, represented by a mutex_t
pointer returned by the thread_create_mutex
function, can be locked with the thread_wait_mutex
function (which waits until the mutex is released) and released with the thread_release_mutex
function. Additionally, the status of a mutex can be independently checked with the thread_test_mutex
function. Mutexes that are no longer to be used should be deallocated with the thread_close_mutex
function.
Parameter |
Description |
---|---|
Return value |
|
Parameter |
Description |
---|---|
|
|
Parameter |
Description |
---|---|
|
|
Return value |
|