Linux thread synchronization

Source: Internet
Author: User
Tags semaphore

Thread synchronization-Mutual exclusion lock 

1. Initialize the mutex lock Pthread_mutex_init ()

int Pthread_mutex_init (pthread_mutex_t *restrict Mutex, const pthread_mutexattr_t *restrict attr);

Cases:

pthread_mutex_t Mutex;

Pthread_mutex_init (&mutex, NULL);

2. Lock the Mutex lock Pthread_mutex_lock ()

int Pthread_mutex_lock (pthread_mutex_t *mutex);

When a thread executes to this function, if the lock is being used by another thread at this time, then this thread is blocked, knowing that another thread is releasing the mutex (the first execution thread can run, not block, and later execute to this function block)

Successfully returns 0, otherwise returns an error number

Example: Pthread_mutex_lock (&mutex);

3. Test Mutex lock Pthread_mutex_trylock ()

int Pthread_mutex_trylock (pthread_mutex_t *mutex);

Lock after test, lock after Trylock

Error returned-1

4. Unlocking

Pthread_mutex_unlock (&mutex);

Successfully returns 0, otherwise returns an error number

5. Destroy the mutex lock

Pthread_mutex_destroy (&mutex);

Thread synchronization-Condition variables 

Customizing conditions to block processes

1. Initialize the condition variable pthread_cond_init ()

int Pthread_cond_int (pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);

The first argument is a pointer to the struct pthread_cond_t, and the second parameter, cond_attr, is a pointer to the structure pthread_condattr_t. The structure pthread_condattr_t is the property structure of the condition variable, and the same as the mutex we can use it to set whether the condition variable is available in process or between processes, the default value is Pthread_process_private, That is, this condition variable is used by each thread within the same process

Cases:

pthread_cond_t cond;

Pthread_cond_init (&cond, NULL);

2. Blocking thread pthread_cond_wait ()

int pthread_cond_wait (pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); (equivalent to a wait function here with a lock blocking this thread, and release another thread of the mutex, until another thread signal wake up and unlock unlocked before continuing to run, automatically unlock, wait for lock, execute to function only blocking, and unlock)

The first parameter is the condition variable, and the second argument is the mutex to be untied when the thread is blocked

Example: Pthread_cond_wait (&cond, &mutex);

3. Wake-Up Thread pthread_cond_signal ()

int pthread_cond_signal (pthread_cond_t *cond); Wakes the thread that is in the wait state, and the awakened thread is in a blocking state until the signal function threads out the mutex and the wait function continues to run

int Pthread_cond_broadcast (pthread_cond_t *cond);

Used to release a thread that is blocked on the condition variable cond, the difference between the two functions is when multiple threads are blocked by the same condition variable:

Use the Pthread_cond_broadcast () function to make all threads wake up

Which thread is woken up with pthread_cond_signal () is determined by the scheduling policy of the thread

Example: Pthread_cond_singal (&cond);

4. Destroying condition variables

Example: Pthread_cond_destroy (&cond);

Thread Synchronization-Semaphore 

1. Initialize semaphore Sem_init ()

int Sem_init (sem_t *sem, int pshared, unsigned value);

The first parameter is a pointer to the semaphore structure

The second parameter is not 0 o'clock this semaphore is shared among processes, otherwise it can be shared only for all threads of the current process

The third parameter gives the initial value of the semaphore

Cases:

sem_t sem;

Sem_init (&sem, 0, 1);

2.PV operation

V Operation

int Sem_post (sem_t *sem);

The parameter is a pointer to the semaphore structure to be added, and when the thread is blocked on the semaphore, calling the Sem_post () function causes one of the threads to be unblocked, and the random selection mechanism of the unblocked thread is also determined by the thread's scheduling policy. When another thread is released, the semaphore is immediately reduced to 0.

P operation

int sem_wait (sem_t *sem);

int sem_trywait (sem_t *sem);

The function sem_wait () is used to block the current thread knowing the value of the semaphore SEM is greater than 0, and the value of the SEM is reduced by 1 after unblocking, indicating that the public resources are decreased after use

The difference between the function sem_trywait () is that when the semaphore equals 0 o'clock, the current thread is not blocked

3. Destruction

int Sem_destroy (sem_t *sem);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux thread synchronization

Related Article

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.