Linux thread Functions
Thread
Create a default thread
Attributes of the default thread:
L not bound
L not separated
L a stack of the default size
L has the same priority as the parent thread
Use phread_attr_init () to create a default property object,
Use the property object to create a thread pthread_create (3 T)
Int P thread_create (pthread_t * tid, const pthread_attr_t * tattr, void * (* start_routine) (void *), void * Arg );
# Include <pthread. h>
Pthread_attr_t tattr;
Pthread_t tid;
Extern void * start_routine (void * Arg );
Void * ARG;
Int ret;
/* Default behavior */
Ret = pthread_create (& tid, null, start_routine, ARG );
/* Init with default attributes */
Ret = pthread_attr_init (& tattr );
/* Default behavior specified */
Ret = pthread_create (& tid, & tattr, start_routine, ARG );
Tattr contains the attributes required to initialize the thread. If the value is null, It is the default value. Start_routine is the starting address of the thread entry function. When start_routine returns, the corresponding thread ends. The exit status value at the end of the thread is the return value returned by the start_routine function using the phread_exit () function. When the pthread_create () function is called successfully, the thread identifier is saved in the TID pointer pointing to the parameter.
Return value. If pthread_create () is successful, 0 is returned,
Eagain exceeds a certain limit, such as excessive lwps.
The value of einval tattr is invalid.
When creating a sub-thread, the input parameter passed to the sub-thread should be the pointer returned by malloc () (such a pointer points to the bucket in the process heap) or the pointer to the global variable, instead of a pointer to a local variable. Because when the sub-thread accesses the input parameter, the function for creating the sub-thread may have ended, and the local variable does not exist.
Wait until the thread ends
Pthread_join (3 T)
Int pthread_join (pthread_t tid, void ** status );
# Include <pthread. h>
Pthread_t tid;
Int ret;
Int status;
/* Waiting to join thread "TID" with status */
Ret = pthread_join (TID, & status );
/* Waiting to join thread "TID" without status */
Ret = pthread_join (TID, null );
Pthread_join () will block the thread that calls it until the end of the thread specified by the TID parameter. The thread specified by TID must be in the current process and must be non-detached. Status receives the status code returned when the specified thread is terminated.
Multiple Threads cannot wait for the end of the same thread; otherwise, the error code esrch is returned.
When pthread_join () is returned, resources such as the stack occupied by the terminating thread have been recycled.
Return Value: 0 is returned for success.
The thread specified by esrch TID is not a valid and unseparated thread in the current thread.
The edeadlk TID specifies the current thread.
The einval TID is invalid.
Separate a thread
Pthread_detach (3 T)
Set a non-detached thread to a detached thread.
Int pthread_detach (pthread_t tid );
# Include <pthread. h>
Pthread_t tid;
Int ret;
Ret = pthread_detach (TID );
This function notifies the thread library that after the thread is terminated, the memory of the thread indicated by TID can be immediately withdrawn.
Return Value: 0 is returned for success.
The einval TID indicates that the thread is not a valid thread.
The thread specified by esrch TID is not a valid and unseparated thread in the current thread.
Create a key for thread data
A multi-threaded C-language program has three types of data: local variables, global variables, and thread data (TSD)
TSD is similar to a global variable, but the thread is private.
Each TSD has a key associated with it.
Pthread_key_create (3 T)
Int pthread_key_create (pthread_key_t * Key, void (* destructor) (* void ));
# Include <pthread. h>
Pthread_key_t key;
Int ret;
/* Key create without destructor */
Ret = pthread_key_create (& Key, null );
/* Key create with destructor */
Ret = pthread_key_destructor (& Key, destructor );
When the function is successful, the configuration is created in the key, and the memory zone pointed to by the key must be valid. Destructor is used to release memory that is no longer needed.
Return Value: 0 is returned for success.
Eagain key namespace exhausted
Enomem does not have enough memory space to create a new key.
Key for deleting thread data
Pthread_key_delete (3 T)
This function is not available in the Solaris thread interface.
Int pthread_key_delete (pthread_key_t key );
# Include <pthread. h>
Pthread_key_t key;
Int ret;
Ret = pthread_key_delete (key );
Before calling this function, you must release the resources associated with this thread. pthread_key_delete () does not trigger a key parsing function.
Return Value: 0 is returned for success.
The einval key value is invalid.
Set the thread data key
Pthread_setspecpacific (3 T)
Set the thread data bound to a thread data key (usually a pointer)
The function sets the pointer value pointing to the private data to the structure indicated by the key;
Int pthread_setspecific (pthread_key_t key, const void * value );
# Include <pthread. h>
Pthread_key_t key;
Void * value;
Int ret;
Ret = pthread_setspecific (Key, value );
Return Value: 0 is returned for success.
Enomem does not have enough virtual memory
The einval key value is invalid.
Pthread_setspecpacific () does not release the memory originally bound to the key. When you bind a key to a new pointer, the memory directed to the original pointer must be released; otherwise, memory leakage may occur.
Obtain the thread data key
Pthread_getspecpacific (3 T)
Obtains the value bound to the thread data key and stores the value at the specified position.
Returns the proprietary pointer stored in the corresponding struct;
Int pthread_getspecific (pthread_key_t key, void ** value)
# Include <pthread. h>
Pthread_key_t key;
Void * value;
Pthread_getspecpacific (Key, & value );
Returned value: no error code is returned.
Obtains the thread ID.
Pthread_self (3 T)
Obtains the identifier of the current thread.
Pthread_t pthread_self (void );
# Include <pthread. h>
Pthread_t tid;
Tid = pthread_self ();
Returned value: the identifier of the current thread.
Comparison thread identifier
Pthread_equal (3 T)
Int pthread_equal (pthread_t tid1, pthread_t tid2 );
# Include <pthread. h>
Pthread_t tid1, tid2
Int ret;
Ret = pthread_equal (tid1, tid2 );
Return Value: If tid1 and tid2 are the same, a non-zero value is returned. Otherwise, 0 is returned. If the parameter is invalid, the returned value is unpredictable.
Initialization thread
Pthread_once (3 T)
Used to call the initialization function. It is valid only for the first call.
Int pthread_once (pthread_once_t * once_control, void (* init_routine) (void ));
# Include <pthread. h>
Pthread_once_t once_control = pthread_once_init;
Int ret;
Ret = pthread_once (& once_control, init_routine );
Once_control defines whether the initialization function has been called.
Return Value: 0 is returned for success.
Einval: a parameter is null.
Control the processor of the current thread
Sched_yeild (3R)
Give the priority of the current thread to a thread with the same or higher priority.
Int sched_yeild (void );
# Include <pthread. h>
Int ret;
Ret = sched_yeild ();
Return Value: 0 is returned for success.
The current enosys version does not support sched_yield ()
Set thread priority
Pthread_setschedparam (3 T)
Int pthread_setschedparam (pthread_t tid, int policy, const struct sched_param * PARAM );
# Include <pthread. h>
Pthread_t tid;
Int ret;
Struct sched_param Param;
Int priority;
/* Sched_priority will be the priority of the thread */
Sched_param, sched_priority = priority;
/* Only supported policy, other will result in enotsup */
Policy = sched_other;
/* Scheduling parameters of target thread */
Ret = pthread_setschedparam (TID, policy, & PARAM );
Return Value: 0 is returned for success.
The einval property value is invalid.
The enotsup attribute value is not supported in the current version.
Obtains the priority of a thread.
Pthread_getschedparam (3 T)
Int pthread_getschedparam (pthread_t tid, int policy, struct schedparam * PARAM );
# Include <pthread. h>
Pthread_t tid;
Sched_param Param;
Int prioprity;
Int policy;
Int ret;
/* Scheduling parameters of target thread */
Ret = pthread_getschedparam (TID, & Policy, & PARAM );
/* Sched_priority contains the priority of the thread */
Priority = Param. sched_priority;
Return Value: 0 is returned for success.
Esrch TID is not an existing thread.
Send signal to thread
Pthread_kill (3 T)
Int pthread_kill (pthread_t tid, int sig );
# Include <pthread. h>
# Include <signal. h>
Int SIG;
Pthread_t tid;
Int ret;
Ret = pthread_kill (TID, sig );
The thread specified by TID must be in the same process as the current thread of the function.
When SIG is 0, an error is checked. If no signal is sent, it is often used to check the validity of TID.
Return Value: 0 is returned for success.
The einval SIG is not a valid semaphore.
Esrch TID is not a thread in the current process
Signal mask used to access the current thread
Pthread_sigmask (3 T)
Int pthread_sigmask (INT how, const sigset_t * New, sigset_t * Old );
# Include <pthread. h>
# Include <signal. h>
Int ret;
Sigset_t old, new;
Ret = pthread_sigmask (sig_setmask, & New, & old );
Ret = pthread_sigmask (sig_block, & New, & old );
Ret = pthread_sigmask (sig_unblock, & New, & old );
How indicates the operation on the current signal mask.
Sig_setmask: Add a new signal set to the signal mask. New indicates the newly added signal to be shielded.
Sig_block: Delete the new signal set in the signal mask. New indicates the newly added signal that does not need to be shielded.
Sig_unblock: Replace the signal mask with the new signal set. New indicates all signals to be shielded.
When new is null, the signal mask of the current thread does not change regardless of how.
The old signal mask is stored in old.
Return Value: 0 is returned for success.
The value of einval how is undefined.
Secure Replication
Pthread_atfork (3 T)
Int pthread_atfork (void (* prepare) (void), void (* parent) (void), void (* Child) (void ));
Terminate thread
Pthread_exit (3 T)
Void pthread_exit (void * status );
# Include <pthread. h>
Int status;
Pthread_exit (& status );
Terminate the current thread, and all memory bound to the thread key will be released. If the current thread is not separated, the identifier and status of the thread are retained until other threads use pthread_join () to wait for the termination of the current thread. If the current thread is separated, the status is ignored, and the thread identifier is immediately withdrawn.
Return Value: If status is not null, the exit code of the thread is set to the value indicated by status.
A thread can terminate its own operation in the following way.
Return from the entry function of the thread.
Call pthread_exit ()
Use POSIX's pthread_cancel ()
To exit, use the following method:
Asynchronous
A series of exit points defined by the thread Library
A series of exit points defined by the application
Exit point
Established by the program through pthread_testcancel ()
The thread that calls pthread_cond_wait () or pthread_cond_timedwait () to wait for a condition
The thread that calls pthread_join () waiting for the end of another thread.
The thread that is blocked on sigwait (2.
Some standard library functions.
Exit thread
Pthread_cancel (3 T)
Int pthread_cancel (pthread_t thread );
# Include <pthread. h>
Pthread_t thread;
Int ret;
Ret = pthread_cancel (thread );
Return Value: 0 is returned for success.
Esrch does not have a specified thread.
Allow or disable exit
Pthread_setcancelstate (3 T)
Exit is allowed by default.
Int pthread_setcancelstate (INT state, int * oldstate );
# Include <pthread. h>
Int oldstate;
Int ret;
/* Enable */
Ret = pthread_setcancelstate (pthread_cancel_enable, & oldstate );
/* Disabled */
Ret = pthread_setcancelstate (pthread_cancel_disable, & oldstate );
Return Value: 0 is returned for success.
The einval state value is invalid.
Set exit type
Pthread_setcanceltype (3 T)
It can be set to the latency or asynchronous type. The default value is the latency type. In asynchronous mode, a thread can be exited at any time during execution.
Int pthread_setcanceltype (INT type, int * oldtype );
# Include <pthread. h>
Int oldtype;
Int ret;
/* Deferred mode */
Ret = pthread_setcanceltype (pthread_cancel_deferred, & oldtype );
/* Async mode */
Ret = pthread_setcanceltype (pthread_cancel_asynchronous, & oldtype );
Return Value: 0 is returned for success.
The einval state value is invalid.
Create exit point
Pthread_testcancel (3 T)
Void pthread_testcancel (void );
# Include <pthread. h>
Pthread_testcancel ();
This parameter is valid only when the exit status of a thread is allowed and the exit type is delayed.
No return value.
Push an aftercare function to exit the stack.
Pthread_cleanup_push (3 T)
Pthread_cleanup_pop (3 T)
Void pthread_cleanup_push (void (* routine) (void *), void * ARGs );
Void pthread_cleanup_pop (INT execute );
# Include <pthread. h>
/* Push the handler "routine" on cleanup stack */
Pthread_cleanup_push (routine, ARG );
/* Pop the "func" out of cleanup stack and execute "func "*/
Pthread_cleanup_pop (1 );
/* Pop the "func" and don't execute "func "*/
Pthread_cleanup_pop (0 );
1. Thread attributes
You can only set attributes when creating an online job, but cannot change it at runtime.
Once the property object is configured, it is valid within the process range.
Initialize attributes
Pthread_attr_init (3 T)
Initialize a thread property object. The attribute value is the default value and the memory occupied is allocated by the thread library.
Int pthread_attr_init (pthread_attr_t * tattr );
# Include <pthread. h>
Pthread_attr_t tattr;
Int ret;
/* Initialize an attribute to the default value */
Ret = pthread_attr_init (& tattr );
Default Value of the property object:
Scope (thread domain) pthread_scope_process
Detachstate (separation status) pthread_create_joinable
Stackaddr (stack address) null
Stacksize (stack size) 1 MB
Priority (priority) parent process priority
Inheritsched (inherited scheduling priority) pthread_inherit_sched
Schedpolicy (scheduling policy) sched_other
Return Value: 0 is returned for success.
Enomem does not have enough memory to initialize the thread property object.
Release property objects
Pthread_attr_destroy (3 T)
Int pthread_attr_destroy (pthread_attr_t * tattr );
# Include <pthread. h>
Pthread_attr_t tattr;
Int ret;
Ret = pthread_attr_destroy (& tattr );
Return Value: 0 is returned for success.
The einval tattr value is invalid.
Set Separation status
Pthread_attr.setdetachstate (3 T)
When a thread is created, if this thread is specified as a separate thread, its thread identifier and other related resources can be used immediately once the thread is terminated. If you do not need to wait for a thread to terminate, you can set it to separate.
Int pthread_attr_setdetachstate (pthread_attr_t * tattr, int detachstate );
# Include <pthread. h>
Pthread_attr_t tattr;
Int ret;
/* Set the thread detach state */
Ret = pthread_attr_setdetachstate (& tattr, pthread_create_detached );
Return Value: 0 is returned for success.
The value of einval tattr or the value of detachstate is invalid.
Separation status
Pthread_attr_getdetachstate (3 T)
Int pthread_attr_getdetachstate (const pthread_attr_t * tattr, int * detachstate );
# Include <pthread. h>
Pthread_attr_t tattr;
Int detachstate;
Int ret;
Ret = pthread_attr_getdetachstate (& tattr, & detachstate );
Return Value: 0 is returned for success.
The value of einval tattr or the value of detachstate is invalid.