Linux Threading Functions Daquan

Source: Internet
Author: User
Tags terminates

Technorati Tags: Linux thread

Index:
1. Create Thread Pthread_create
2. Wait for thread to end Pthread_join
3. Detach Thread Pthread_detach
4. Create line Cheng Pthread_key_create
5. Strikethrough Cheng Pthread_key_delete
6. Set Thread data pthread_setspecific
7. Get Thread Data pthread_getspecific
8. Get the thread identifier Pthread_self
9. Compare Thread Pthread_equal
10. One-time implementation of Pthread_once
11. Assignment of executive power Sched_yield
12. Modify the priority level Pthread_setschedparam
13. Get Priority Pthread_getschedparam
14. Send Signal Pthread_kill
15. Set the thread mask Pthread_sigmask
16. Terminating Thread Pthread_exit
17. Exit Thread Pthread_cancel
18. Allow/Disallow exit thread Pthread_setcancelstate
19. Set the exit type Pthread_setcanceltype
20. Create an exit point Pthread_testcancel
21. Press into the aftercare function
22. Popup Aftercare function
--------------------------------------------------------------------------------
1. Create Thread Pthread_create
#include
int Pthread_create (pthread_t *tid, const pthread_attr_t *tattr, void * (*start_routine) (void *), void *arg);
Return value: The function returned 0 successfully. Any other return value represents an error.
Creates a thread.
The parameter tattr contains the properties required to initialize the thread, Start_routine is the address of the thread entry function, and when Start_routine returns, the corresponding thread ends.
When the function succeeds, the thread marks the characters in the memory that the parameter tid points to.
If you do not specify a Property object and set it to NULL, a default thread is created with the following properties:
Non-binding;
Not separated;
By a default-sized stack;
Has the same priority as the parent thread.
Note: When you create a child thread, the input parameter passed to the child thread is preferably a pointer returned by the malloc () function or a pointer to a global variable, rather than a pointer to a local variable. The zone is still valid to ensure that the child threads process the parameters.
--------------------------------------------------------------------------------
2. Wait for thread to end Pthread_join
#include
int Pthread_join (pthread_t tid, void **status);
Return value: The function returned 0 successfully. Any other return value represents an error.
Wait for a thread to end.
The function blocks calls to its thread until the parameter tid specifies the end of the thread.
The specified thread of the TID must be in the current process, while the specified thread of the TID must be non-detached.
You cannot have multiple threads waiting for the same thread to terminate. If this occurs, one thread will return successfully, and the other thread will return error esrch.
If the parameter status is not NULL, the exit status of the thread is placed in the memory that the status points to.
--------------------------------------------------------------------------------
3. Detach Thread Pthread_detach
#include
int Pthread_detach (pthread_t tid);
Return value: The function returned 0 successfully. Any other return value represents an error.
Sets a non-detached thread to detach the thread. That is, the notification line libraries resources such as memory consumed by threads when the specified thread terminates.
The result of using multiple Pthread_detach on one thread is not predictable.
--------------------------------------------------------------------------------
4. Create line Cheng Pthread_key_create
#include
int Pthread_key_create (pthread_key_t *key, Void (*destructor) (void*));
Return value: The function returned 0 successfully. Any other return value represents an error.
Assigns a key value to the process that is used to represent a thread data item. This key is visible to all threads in the process. When you create a thread data key, the value associated with the key in all threads is null.
After the function returns successfully, the assigned key is placed in the memory that the key parameter points to, and the memory area that the key parameter points to is guaranteed to be valid.
If the parsing function destructor is specified, then when the thread ends and the non-null value is bound to the key, the system calls the destructor function, which is the value that the associated thread binds to the key. The block of memory bound to this key can be freed by the destructor function.
--------------------------------------------------------------------------------
5. Strikethrough Cheng Pthread_key_delete
#include
int Pthread_key_delete (pthread_key_t key);
Return value: The function returned 0 successfully. Any other return value represents an error.
Deletes the thread data key. The memory that this key occupies is freed, and the key is then referenced to return an error.
Before calling this function, the program must release the resource associated with this line threads, which does not raise the parsing function of the thread data key.
--------------------------------------------------------------------------------
6. Set Thread data pthread_setspecific
#include
int pthread_setspecific (pthread_key_t key, const void *value);
Return value: The function returned 0 successfully. Any other return value represents an error.
Sets the thread-specific data (typically pointers) that is bound to a thread's data key.
The function does not release memory that was originally bound to the key, and when a new pointer is bound to a key value, the memory pointed to by the original pointer must be released, or a memory leak will occur.
--------------------------------------------------------------------------------
7. Get Thread Data pthread_getspecific
#include
void Pthread_getspecific (pthread_key_t key, void **value);
no return value. When an error occurs, value points to null.
Gets the value that is bound on the thread data key and stores the fetched value at the specified location.
--------------------------------------------------------------------------------
8. Get the thread identifier Pthread_self
#include
pthread_t pthread_self (void);
Returns the identifier of the current thread.
--------------------------------------------------------------------------------
9. Compare Thread Pthread_equal
#include
int pthread_equal (pthread_t tid1, pthread_t Tid2);
If Tid1 and Tid2 are the same, the function returns a value other than 0, otherwise returns 0.
If either TID1 or TID2 is an illegal value, the return will be unpredictable.
--------------------------------------------------------------------------------
10. One-time implementation of Pthread_once
#include
int pthread_once (pthread_once_t *once_control, Void (*init_routine) (void));
Return value: The function returned 0 successfully. Any other return value represents an error.
function is used to invoke the initialization function. If you have already called this initialization function through Pthread_once, then calling this initialization function later through the Pthread_once function will be invalid.
The parameter Once_control determines whether the corresponding initialization function has been called. It is generally used as follows:
[static] pthread_once_t Once_control = pthread_once_init.
--------------------------------------------------------------------------------
11. Assignment of executive power Sched_yield
#include
int Sched_yield (void);
Return value: The function returned 0 successfully. -1 indicates an error.
The execution of the current thread (that is, control over the processor) is given to another thread with the same or higher priority.
--------------------------------------------------------------------------------
12. Modify the priority level Pthread_setschedparam
#include
int Pthread_setschedparam (pthread_t tid, int policy, const struct Sched_param *param);
Return value: The function returned 0 successfully. Any other return value represents an error.
Modifies the priority of a thread.
--------------------------------------------------------------------------------
13. Get Priority Pthread_getschedparam
#include
int Pthread_getschedparam (pthread_t tid, int policy, struct schedparam *param);
Return value: The function returned 0 successfully. Any other return value represents an error.
Gets the priority level of the thread.
--------------------------------------------------------------------------------
14. Send Signal Pthread_kill
#include
int Pthread_kill (pthread_t tid, int sig);
Return value: The function returned 0 successfully. Any other return value represents an error.
Sends a signal to the thread specified by the TID that the specified thread must be in the same process as the current thread.
When the sig parameter is 0 o'clock, the function makes an error check and does not send a signal, which is often used to check the legitimacy of the TID.
--------------------------------------------------------------------------------
15. Set the thread mask Pthread_sigmask
#include
#include
int pthread_sigmask (int how, const sigset_t *new, sigset_t *old);
Return value: The function returned 0 successfully. Any other return value represents an error.
Alters or verifies the signal mask of the current thread.
The parameter how indicates what to do with the current signal mask, with the following values: Sig_block, Sig_unblock, Sig_setmask.
When the parameter new is null, the current thread's signal mask does not change regardless of what the value is.
The old signal mask is saved in memory that is pointed to by the parameter, and is not NULL.
--------------------------------------------------------------------------------
16. Terminating Thread Pthread_exit
#include
void Pthread_exit (void *status);
Terminates the current thread, and any memory bound on the threads Data key will be freed. If the current thread is non-detached, then the label of the thread that conforms to the exit code is retained until other threads use Pthread_join to wait for the current thread to terminate. If the current thread is detached, status is ignored and the thread identifier is immediately recycled.
If status is not NULL, the thread's exit code is set to the value pointed to by the status parameter.
--------------------------------------------------------------------------------
17. Exit Thread Pthread_cancel
#include
int Pthread_cancel (pthread_t thread);
Return value: The function returned 0 successfully. Any other return value represents an error.
Exits a thread. How you respond to an exit request depends on the status of the target thread.
--------------------------------------------------------------------------------
18. Allow/Disallow exit thread Pthread_setcancelstate
#include
int pthread_setcancelstate (int state, int *oldstate);
Return value: The function returned 0 successfully. Any other return value represents an error.
The parameter state takes a value of pthread_cancel_enable or pthread_cancel_disable.
--------------------------------------------------------------------------------
19. Set the exit type Pthread_setcanceltype
#include
int pthread_setcanceltype (int type, int *oldtype);
Return value: The function returned 0 successfully. Any other return value represents an error.
Sets the thread exit type to either a deferred type or an asynchronous type. The value of the parameter type is pthread_cancel_deferred or pthread_cancel_asynchronous.
When a thread is created, the default value is the delay type. In an asynchronous manner, a thread can be exited at any time it executes.
--------------------------------------------------------------------------------
20. Create an exit point Pthread_testcancel
#include
void Pthread_testcancel (void);
no return value.
Sets the exit point for the thread.
This function is called only if the exit status of the thread is allowed to exit, and if the thread's exit type is deferred. If the exit state of the thread is forbidden when called, the call does not work.
Be careful with this function, you can set the exit point only where it is safe to exit.
--------------------------------------------------------------------------------
21. Press into the aftercare function
#include
void Pthread_cleanup_push (void (*routine) (void *), void *args);
A aftercare function is pressed into the aftermath processing function stack.
--------------------------------------------------------------------------------
22. Popup Aftercare function
#include
void Pthread_cleanup_pop (int execute);
A aftercare function is popped out of the aftermath processing function stack. If the parameter execute is not 0, the popup function is executed, and if the argument is 0, the popup function is not executed.
If a line Cheng or implicitly calls the Pthread_exit () function or thread to accept an exit request, the line libraries will actually call the Pthread_cleanup_pop function with a non-0 parameter.

Linux Threading Functions Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.