multithreaded thread-related functions under Linux

Source: Internet
Author: User

Multithreading some basic functions and identifiers

(1) pthread_t:unsigned long int is a thread identifier

(2) Pthread_create function for creating threads

int Pthread_create (pthread_t *_thread,//The first argument points to the thread identifier of a pointer

__const pthread_attr_t *__attr//The second parameter is a thread property

void * (*__start_routine) (void *)//start address of the thread run function

void *__arg//arguments for thread functions

The second and fourth parameters are generally set to null

Returns 0 if thread creation succeeds, otherwise returns other values, common Eagain system limits create thread einval second argument is illegal

The newly created thread runs the thread function, while the previous thread continues to run the next line of code.

(3) Pthread_join function waits for the end of a thread

extern int Pthread_join __p ((pthread_t __th, void **__thread_return));
First parameter: The thread identifier being waited on, the thread ID

Second parameter: A return value that can be used to store the thread being waited on.

This function is a thread blocking function, and the function that invokes it will wait until the thread that is waiting is terminated.

(4) Pthread_exit

extern void Pthread_exit __p ((void *__retval)) __attribute__ ((__noreturn__));

There are two ways to end a thread, one is like our example above, the function ends, and the thread that calls it ends. ;

Another type of party is implemented by the function Pthread_exit-

Mutexes are used to guarantee that only one thread is executing a piece of code over a period of time.
A pthread_mutex_init
The function pthread_mutex_init is used to generate a mutex lock. A NULL parameter indicates that the default property is used. If you need
To declare a mutex for a particular property, call the function Pthread_mutexattr_init. Function
The pthread_mutexattr_setpshared and function Pthread_mutexattr_settype are used to set the mutex lock genus
Of The previous function sets the property pshared, which has two values, pthread_process_private and
Pthread_process_shared. The former is used for thread synchronization in different processes, and the latter is used to synchronize the different of this process
Thread. In the example above, we are using the default attribute Pthread_process_ PRIVATE. The latter is used to set
Type of mutex, optional types are pthread_mutex_normal, Pthread_mutex_errorcheck,
Pthread_mutex_recursive and PTHREAD _mutex_default. They define different top, unlocked
Mechanism, in general, selects the last default attribute.

Two Pthread_mutex_lock Pthread_mutex_unlock pthread_delay_np

The Pthread_mutex_lock declaration begins with a mutex lock, and thereafter the code continues until the call to Pthread_mutex_unlock is reached.

Are locked, that is, only one thread can invoke execution at the same time. When a thread executes to the Pthread_mutex_lock,

If the lock is being used by another thread at this point, the thread is blocked, that is, the program waits for the other thread to release the mutex.

multithreaded thread-related functions under Linux

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.