Linux thread-Function

Source: Internet
Author: User
Index:

1. Create thread pthread_create
2. Wait for the thread to end pthread_join
3. Separate the thread pthread_detach
4. Create a thread key pthread_key_create
5. Delete the line key pthread_key_delete
6. Set the thread data pthread_setspecific
7. Get the thread data pthread_getspecific
8. Get the thread identifier pthread_self
9. Compare the thread pthread_equal
10. One execution of pthread_once
11. Transfer execution permission sched_yield
12. Modify the priority pthread_setschedparam
13. Get the priority pthread_getschedparam
14. pthread_kill
15. Set the thread mask pthread_sigmask
16. Terminate the thread pthread_exit
17. Exit the thread pthread_cancel
18. Allow/prohibit exit from the thread pthread_setcancelstate
19. Set the exit type pthread_setcanceltype
20. Create an exit point pthread_testcancel
21. push into the aftercare Function
22. The aftercare function is displayed.


1. Create thread pthread_create

# Include <pthread. h>
Int pthread_create (pthread_t * tid, const pthread_attr_t * tattr, void * (* start_routine) (void *), void * Arg );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Create a thread.

The tattr parameter contains the attributes required by the initialization thread. start_routine is the address of the thread entry function. When start_routine returns, the corresponding thread ends.

When the function succeeds, the thread identifier is saved in the memory pointed to by the TID parameter.

If the attribute object is not specified and set to null, a default thread is created with the following attributes:

  • Not bound;
  • Unseparated;
  • A stack of the default size;
  • Has the same priority as the parent thread.

Note: When creating a sub-thread, the input parameter sent to the sub-thread is preferably a pointer returned by the malloc () function or a pointer to a global variable, instead of a pointer to a local variable. This region is still valid when the sub-thread processes parameters.

2. Wait for the thread to end pthread_join

# Include <pthread. h>
Int pthread_join (pthread_t tid, void ** status );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Wait for a thread to end.

This function blocks the call to its thread until the thread specified by the TID parameter ends.

The thread specified by TID must be in the current process, and the thread specified by TID must be non-separated.

Multiple Threads cannot wait for the same thread to terminate. In this case, one thread will return success, and other threads will return the error esrch.

If the parameter status is not null, the exit status of the thread is placed in the memory that the status points.


3. Separate the thread pthread_detach

# Include <pthread. h>
Int pthread_detach (pthread_t tid );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Set a non-detached thread to a detached thread. That is, the notification thread library recycles the memory and other resources occupied by the thread when the specified thread is terminated.

The results of using pthread_detach multiple times on one thread are unpredictable.


4. Create a thread key pthread_key_create

# Include <pthread. h>
Int pthread_key_create (pthread_key_t * Key, void (* destructor) (void *));
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Assign a key value in the process, which is used to represent a thread data item. This key is visible to all threads in the process. When the thread data key is created, the values associated with the key in all threads are null.

After the function is successfully returned, the allocated key is placed in the memory to which the key parameter points. Ensure that the key parameter points to the memory zone.

If destructor is specified, when the thread ends and the non-null value is bound to this key, the system calls the destructor function, the parameter is the value bound by the relevant thread to this key. The memory block bound to this key can be released by the destructor function.


5. Delete the line key pthread_key_delete

# Include <pthread. h>
Int pthread_key_delete (pthread_key_t key );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Deletes the thread data key. The memory occupied by this key will be released, and an error will be returned if the key is referenced again.

Before calling this function, the program must release the resources associated with the thread. This function does not trigger the parsing function of the thread data key.


6. Set the thread data pthread_setspecific

# Include <pthread. h>
Int pthread_setspecific (pthread_key_t key, const void * value );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Set the thread-specific data (usually a pointer) that is bound with a thread data key ).

The function will not release the memory originally bound to the key. When you bind a key value to a new pointer, you must release the memory that the original Pointer Points to. Otherwise, the memory will leak.


7. Get the thread data pthread_getspecific

# Include <pthread. h>
Void pthread_getspecific (pthread_key_t key, void ** value );
No return value. When an error occurs, the value points to null.

Obtains the value bound to the thread data key and stores the obtained value at the specified position.


8. Get the thread identifier pthread_self

# Include <pthread. h>
Pthread_t pthread_self (void );

Returns the identifier of the current thread.


9. Compare the thread pthread_equal

# Include <pthread. h>
Int pthread_equal (pthread_t tid1, pthread_t tid2 );
If tid1 and tid2 are the same, the function returns a non-0 value. Otherwise, 0 is returned.

If any of tid1 or tid2 is an invalid value, the return result is unpredictable.


10. One execution of pthread_once

# Include <pthread. h>
Int pthread_once (pthread_once_t * once_control, void (* init_routine) (void ));
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

The function is used to call the initialization function. If you have already called this initialization function once through pthread_once, it will be invalid to call this initialization function through pthread_once later.

The once_control parameter determines whether the initialization function has been called. It is generally used as follows:
[Static] pthread_once_t once_control = pthread_once_init.


11. Transfer execution permission sched_yield

# Include
Int sched_yield (void );
Return Value: 0 is returned if the function is successful. -1 indicates an error.

Assign the execution right of the current thread (that is, control of the processor) to another thread with the same or higher priority.


12. Modify the priority pthread_setschedparam

# Include <pthread. h>
Int pthread_setschedparam (pthread_t tid, int policy, const struct sched_param * PARAM );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Modifies the priority of a thread.


13. Get the priority pthread_getschedparam

# Include <pthread. h>
Int pthread_getschedparam (pthread_t tid, int policy, struct schedparam * PARAM );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Obtains the priority of a thread.


14. pthread_kill

# Include <pthread. h>
Int pthread_kill (pthread_t tid, int sig );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Sends a signal to the thread specified by TID. The thread specified by TID must be in the same process as the current thread.

When the SIG parameter is 0, the function checks errors and does not send signals. This is often used to check the validity of TID.


15. Set the thread mask pthread_sigmask

# Include <pthread. h>
# Include <signal. h>
Int pthread_sigmask (INT how, const sigset_t * New, sigset_t * Old );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Change or verify the signal mask of the current thread.

The how parameter indicates the operation on the current signal mask, which has the following values: sig_block, sig_unblock, and sig_setmask.

When the new parameter is null, the signal mask of the current thread does not change no matter what the value of how.

The old signal mask is stored in the memory to which the old parameter points. If old is not null.


16. Terminate the thread pthread_exit

# Include <pthread. h>
Void pthread_exit (void * status );

Terminate the current thread. All memory bound to the thread data key will be released. If the current thread is not separated, the exit code for this thread will be retained until other threads use pthread_join to wait for the termination of the current thread. If the current thread is separated, the status will be ignored and the thread identifier will be recycled immediately.

If the status is not null, the exit code of the thread is set to the value pointed to by the status parameter.


17. Exit the thread pthread_cancel

# Include <pthread. h>
Int pthread_cancel (pthread_t thread );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Exit A thread. The response to the exit request depends on the status of the target thread.


18. Allow/prohibit exit from the thread pthread_setcancelstate

# Include <pthread. h>
Int pthread_setcancelstate (INT state, int * oldstate );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

The value of the state parameter is pthread_cancel_enable or pthread_cancel_disable.


19. Set the exit type pthread_setcanceltype

# Include <pthread. h>
Int pthread_setcanceltype (INT type, int * oldtype );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Set the thread exit type to the latency or asynchronous type. The value of type is pthread_cancel_deferred or pthread_cancel_asynchronous.

When a thread is created, the default value is the delay type. In asynchronous mode, a thread can be exited at any time of execution.


20. Create an exit point pthread_testcancel

# Include <pthread. h>
Void pthread_testcancel (void );
No return value.

Set the exit point of the thread.

This function is valid only when the exit status of a thread is allowed and the exit type of the thread is delayed. If the thread exit status is disabled during the call, the call does not take effect.

Use this function with caution. You can set the exit point only when the function is safely exited.


21. push into the aftercare Function

# Include <pthread. h>
Void pthread_cleanup_push (void (* routine) (void *), void * ARGs );

Push an aftercare function into the aftercare function stack.


22. The aftercare function is displayed.

# Include <pthread. h>
Void pthread_cleanup_pop (INT execute );

An aftercare function is displayed from the aftercare function stack. If the execute parameter is not 0, the pop-up function is executed. If the parameter is 0, the pop-up function is not executed.

If a thread explicitly or implicitly calls the pthread_exit () function or the thread accepts the exit request, the thread library will actually call the pthread_cleanup_pop function with a non-zero parameter.

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.