Thread Creation and Management
◆ M_THREAD_PRIORITY_MIN
#define M_THREAD_PRIORITY_MIN 1 |
Minimum thread priority value
◆ M_THREAD_PRIORITY_NORMAL
#define M_THREAD_PRIORITY_NORMAL 5 |
Normal thread priority value
◆ M_THREAD_PRIORITY_MAX
#define M_THREAD_PRIORITY_MAX 9 |
Maximum thread priority value
◆ M_threadid_t
Thread id used to identify a thread.
This can be compared with >, <, == and !=.
◆ M_thread_attr_t
◆ M_thread_create()
Create and run a thread.
Threads are created detached by default. To create it joinable use a M_thread_attr_t and set it to joinable.
- Parameters
-
[in] | attr | Thread creation attributes. |
[in] | func | The function to run. |
[in,out] | arg | Argument to pass to func. |
- Returns
- Threadid identifying the thread on success. Threadid will be 0 on failure.
◆ M_thread_join()
Wait for a thread to finish.
Only threads that were created with the joinable attribute set to M_TRUE can be used with this function.
- Parameters
-
[in] | id | The threadid to wait on. |
[out] | value_ptr | The return value from the thread. |
- Returns
- M_TRUE if the thread was successfully joined. Otherwise M_FALSE.
◆ M_thread_self()
Get the threadid of the running thread.
- Returns
- The threadid.
◆ M_thread_set_priority()
M_bool M_thread_set_priority |
( |
M_threadid_t |
tid, |
|
|
M_uint8 |
priority |
|
) |
| |
Set the priority a given thread should be created with.
- Parameters
-
[in] | tid | ThreadID returned from M_thread_create() or M_thread_self() |
[in] | priority | The priority to set. Valid range is 1-9 with 1 being the lowest priority and 9 being the highest. The default value is 5. Some systems, like Linux, do not support thread scheduling in relation to the process as a whole, but rather the system as a whole, and therefore require RLIMIT_NICE to be configured on the process in order to successfully increase a thread's priority above '5'. |
- Returns
- M_TRUE on success, or M_FALSE on usage error
◆ M_thread_set_processor()
M_bool M_thread_set_processor |
( |
M_threadid_t |
tid, |
|
|
int |
processor_id |
|
) |
| |
Set the processor to assign the thread to run on (aka affinity). The range is 0 to M_thread_num_cpu_cores()-1, or -1 to unassign.
- Parameters
-
- Returns
- M_TRUE on success, or M_FALSE on usage error
◆ M_thread_sleep()
void M_thread_sleep |
( |
M_uint64 |
usec | ) |
|
Sleep for the specified amount of time.
- Parameters
-
[in] | usec | Number of microseconds to sleep. |
◆ M_thread_yield()
void M_thread_yield |
( |
M_bool |
force | ) |
|
Inform the scheduler that we want to relinquish the CPU and allow other threads to process.
- Parameters
-
[in] | force | Force rescheduling of this thread. When M_FALSE the thread model will determine if the thread needs to be rescheduled or not. A preemtive model will typically ignore this call when M_FALSE and rely on its scheduler. A non-preemptive model (COOP) will always yield. |
◆ M_thread_attr_create()
Create a thread attribute object.
- Returns
- Thread attribute object.
◆ M_thread_attr_destroy()
Destroy a thread attribute object.
- Parameters
-
[in] | attr | Attribute object. |
◆ M_thread_attr_get_create_joinable()
Get whether a given thread should be created joinable.
- Parameters
-
[in] | attr | Attribute object. |
return M_TRUE if the thread should be joinable. Otherwise M_FALSE.
◆ M_thread_attr_get_stack_size()
Get the stack size a given thread should use when created.
This may not be used by all threading models.
- Parameters
-
[in] | attr | Attribute object. |
return The requested stack size.
◆ M_thread_attr_get_priority()
Get the priority a given thread should be created with.
Thread priorities are 1-9, with 1 being the lowest priority and 9 being the highest. The default value is 5.
- Parameters
-
[in] | attr | Attribute object. |
- Returns
- The requested priority, or 0 on usage error.
◆ M_thread_attr_set_create_joinable()
void M_thread_attr_set_create_joinable |
( |
M_thread_attr_t * |
attr, |
|
|
M_bool |
val |
|
) |
| |
Set whether a given thread should be created joinable.
The default is to create threads detached (not joinable) unless this is called and set to M_TRUE.
- Parameters
-
[in] | attr | Attribute object. |
[in] | val | The value to set. |
◆ M_thread_attr_set_stack_size()
Set the stack size a given thread should be created with.
- Parameters
-
[in] | attr | Attribute object. |
[in] | val | The value to set. |
◆ M_thread_attr_set_priority()
M_bool M_thread_attr_set_priority |
( |
M_thread_attr_t * |
attr, |
|
|
M_uint8 |
priority |
|
) |
| |
Set the priority a given thread should be created with.
- Parameters
-
[in] | attr | Attribute object. |
[in] | priority | The priority to set. Valid range is 1-9 with 1 being the lowest priority and 9 being the highest. The default value is 5. Some systems, like Linux, do not support thread scheduling in relation to the process as a whole, but rather the system as a whole, and therefore require RLIMIT_NICE to be configured on the process in order to successfully increase a thread's priority above '5'. |
- Returns
- M_TRUE on success, or M_FALSE on usage error
◆ M_thread_attr_get_processor()
Get the currently assigned processor for thread.
- Parameters
-
- Returns
- -1 if none specified, otherwise 0 to M_thread_num_cpu_cores()-1
◆ M_thread_attr_set_processor()
M_bool M_thread_attr_set_processor |
( |
M_thread_attr_t * |
attr, |
|
|
int |
processor_id |
|
) |
| |
Set the processor to assign the thread to run on (aka affinity). The range is 0 to M_thread_num_cpu_cores()-1.
- Parameters
-
[in] | attr | Attribute object |
[in] | processor_id | -1 to unset prior value. Otherwise 0 to M_thread_num_cpu_cores()-1 is the valid range. |
- Returns
- M_TRUE on success, or M_FALSE on usage error