Mstdlib-1.24.0
Thread Creation and Management

Macros

#define M_THREAD_PRIORITY_MIN   1
 
#define M_THREAD_PRIORITY_NORMAL   5
 
#define M_THREAD_PRIORITY_MAX   9
 

Typedefs

typedef M_uintptr M_threadid_t
 
typedef struct M_thread_attr M_thread_attr_t
 

Functions

M_threadid_t M_thread_create (const M_thread_attr_t *attr, void *(*func)(void *), void *arg)
 
M_bool M_thread_join (M_threadid_t id, void **value_ptr)
 
M_threadid_t M_thread_self (void)
 
M_bool M_thread_set_priority (M_threadid_t tid, M_uint8 priority)
 
M_bool M_thread_set_processor (M_threadid_t tid, int processor_id)
 
void M_thread_sleep (M_uint64 usec)
 
void M_thread_yield (M_bool force)
 
M_thread_attr_tM_thread_attr_create (void)
 
void M_thread_attr_destroy (M_thread_attr_t *attr)
 
M_bool M_thread_attr_get_create_joinable (const M_thread_attr_t *attr)
 
size_t M_thread_attr_get_stack_size (const M_thread_attr_t *attr)
 
M_uint8 M_thread_attr_get_priority (const M_thread_attr_t *attr)
 
void M_thread_attr_set_create_joinable (M_thread_attr_t *attr, M_bool val)
 
void M_thread_attr_set_stack_size (M_thread_attr_t *attr, size_t val)
 
M_bool M_thread_attr_set_priority (M_thread_attr_t *attr, M_uint8 priority)
 
int M_thread_attr_get_processor (const M_thread_attr_t *attr)
 
M_bool M_thread_attr_set_processor (M_thread_attr_t *attr, int processor_id)
 

Detailed Description

Thread Creation and Management

Macro Definition Documentation

◆ 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

Typedef Documentation

◆ M_threadid_t

typedef M_uintptr M_threadid_t

Thread id used to identify a thread.

This can be compared with >, <, == and !=.

◆ M_thread_attr_t

typedef struct M_thread_attr M_thread_attr_t

Function Documentation

◆ M_thread_create()

M_threadid_t M_thread_create ( const M_thread_attr_t attr,
void *(*)(void *)  func,
void *  arg 
)

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]attrThread creation attributes.
[in]funcThe function to run.
[in,out]argArgument to pass to func.
Returns
Threadid identifying the thread on success. Threadid will be 0 on failure.

◆ M_thread_join()

M_bool M_thread_join ( M_threadid_t  id,
void **  value_ptr 
)

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]idThe threadid to wait on.
[out]value_ptrThe return value from the thread.
Returns
M_TRUE if the thread was successfully joined. Otherwise M_FALSE.

◆ M_thread_self()

M_threadid_t M_thread_self ( void  )

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]tidThreadID returned from M_thread_create() or M_thread_self()
[in]priorityThe 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
[in]tidThreadID returned from M_thread_create() or M_thread_self()
[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

◆ M_thread_sleep()

void M_thread_sleep ( M_uint64  usec)

Sleep for the specified amount of time.

Parameters
[in]usecNumber 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]forceForce 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()

M_thread_attr_t * M_thread_attr_create ( void  )

Create a thread attribute object.

Returns
Thread attribute object.

◆ M_thread_attr_destroy()

void M_thread_attr_destroy ( M_thread_attr_t attr)

Destroy a thread attribute object.

Parameters
[in]attrAttribute object.

◆ M_thread_attr_get_create_joinable()

M_bool M_thread_attr_get_create_joinable ( const M_thread_attr_t attr)

Get whether a given thread should be created joinable.

Parameters
[in]attrAttribute object.

return M_TRUE if the thread should be joinable. Otherwise M_FALSE.

◆ M_thread_attr_get_stack_size()

size_t M_thread_attr_get_stack_size ( const M_thread_attr_t attr)

Get the stack size a given thread should use when created.

This may not be used by all threading models.

Parameters
[in]attrAttribute object.

return The requested stack size.

◆ M_thread_attr_get_priority()

M_uint8 M_thread_attr_get_priority ( const M_thread_attr_t attr)

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]attrAttribute 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]attrAttribute object.
[in]valThe value to set.

◆ M_thread_attr_set_stack_size()

void M_thread_attr_set_stack_size ( M_thread_attr_t attr,
size_t  val 
)

Set the stack size a given thread should be created with.

Parameters
[in]attrAttribute object.
[in]valThe 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]attrAttribute object.
[in]priorityThe 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()

int M_thread_attr_get_processor ( const M_thread_attr_t attr)

Get the currently assigned processor for thread.

Parameters
[in]attrAttribute object
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]attrAttribute 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