Pthread thread library-nptl

Source: Internet
Author: User

Pthread is a set of user-level thread libraries, but the kernel-level threads are used for implementation on Linux to improve thread concurrency. pthread is a set of common thread libraries provided by POSIX and has good portability.

 

Used to create a new thread:

Int pthread_create (pthread_t * thread, pthread_attr_t * ATTR, void * (* start_routine) (void *), void * Arg );

The first parameter is used to save the thread ID, which will be used for subsequent operations on the thread;

The second parameter is used to describe the attributes of the thread to be created. If it is set to null, the default attribute is used;

The third parameter specifies the thread entry, which is a function with only one (void *) parameter;

The fourth parameter specifies the parameter to be passed to the thread entry function.

 

The return value of each thread is void *, which can be returned in two ways:

1. Return pointer;

2. pthread_exit (pointer );

Other threads return values through the following functions:

Int pthread_join (pthread_t th, void ** thread_return );

A thread has two States. joinable indicates that the system retains the return value of the thread until another thread removes it. The detach system does not retain the return value.

Int pthread_detach (pthread_t th );

 

 

 

Mutex solves the problem of mutual exclusion:

Mutex is a mutex device used to protect critical zones and shared memory. It has two states: Locked and unlocked, which cannot be owned by both threads at the same time.

Initialize a mutex:

Int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * mutexattr );

Lock a mutex:

Int pthread_mutex_lock (pthread_mutex_t * mutex ));

Try to lock a mutex:

Int pthread_mutex_trylock (pthread_mutex_t * mutex );

Unlock a mutex:

Int pthread_mutex_unlock (pthread_mutex_t * mutex );

Destroy a mutex:

Int pthread_mutex_destroy (pthread_mutex_t * mutex );

 

There are three types of locks: "fast", "recursive" or "error checking"

1) Lock operation:

A. if it is in the unlock status, lock it so that it belongs to itself.

B. When it is locked by other threads, the thread is suspended until it is unlocked by other threads.

C. If the current process has been locked by the user, "fast" suspends the current process, "recursive" succeeds, and immediately returns the number of locked times. "error checking" immediately returns edeadlk.

 

2) during the unlock operation:

A. "fast" wake up the first locked thread;

B. "recursive" reduces the number of locks (it is locked by itself, regardless of other threads). When the number of locks is equal to 0, it is unlocked and the first locked thread is awakened.

C. "error checking" checks whether it is locked by itself. If not, eperm is returned. If yes, the first locked thread is awakened.

We usually use some static variables to initialize mutex:

"Fast" 'pthread _ mutex_initializer'

"Recursive" 'pthread _ recursive_mutex_initializer_np'

"Error checking" 'pthread _ errorcheck_mutex_initializer_np'

 

 

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.