Multithreading in Linux environments with multithreaded programming (II.)

Source: Internet
Author: User
Tags semaphore

In the previous article, we mainly explained the basic concepts and features of multithreading in Linux environment, this article will explain the synchronization mode of multithreading in Linux environment.

In the second edition of the UNIX Environment Advanced Programming, Chapter 11th threads, three basic synchronization mechanisms are mentioned: mutex, read-write lock, condition variable . The following are descriptions of these three mechanisms:

One, thread mutex

Mutual exclusion implies "exclusive", that is, two threads cannot enter mutually exclusive protected code at the same time. Linux can be defined by the pthread_mutex_t mutex mechanism to complete the multi-threaded mutex, the role of the mechanism is to a mutually exclusive part, in the first to obtain a mutex, if not get the mutex, that the mutex is owned by other threads, at this time to get the mutex thread blocking, Until the thread that owns the mutex completes the mutex operation. The operation functions of the mutex include:

#include <pthread.h>intconst pthread_mutexattr_t, *mutexattr); int pthread_mutex_lock (pthread_mutex_t *mutex); int pthread_mutex_unlock (pthread_mutex_t *mutex); int pthread_mutex_destory (pthread_mutex_t *mutex);

As with other functions, these functions return 0 when they succeed, and error codes are returned when they fail, but these functions do not set errno, so the return code of the function must be checked. Here is an example of how to use:

#include <pthread.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<semaphore.h>#defineSIZE 1024CharBuffer[size];void*thread_function (void*Arg);p thread_mutex_t mutex;intMain () {intRes;    pthread_t A_thread; void*Thread_result; Res= Pthread_mutex_init (&mutex, NULL); if(Res! =0) {perror ("Mutex Init failed!");    Exit (Exit_failure); } Res= Pthread_create (&a_thread, NULL, thread_function, NULL); if(Res! =0) {perror ("Thread Create failed!");    Exit (Exit_failure); } printf ("Input some text. Enter ' End ' to finish/n");  while(1) {Pthread_mutex_lock (&mutex); scanf ("%s", buffer); Pthread_mutex_unlock (&mutex); if(STRNCMP ("End", Buffer,3) ==0)             Break; Sleep (1); } Res= Pthread_join (A_thread, &Thread_result); if(Res! =0) {perror ("Thread Join failed!");    Exit (Exit_failure); } printf ("Thread joined/n"); Pthread_mutex_destroy (&mutex); Exit (exit_success);}void*thread_function (void*Arg) {Sleep (1);  while(1) {Pthread_mutex_lock (&mutex); printf ("You input%d characters/n", strlen (buffer)); Pthread_mutex_unlock (&mutex); if(STRNCMP ("End", Buffer,3) ==0)             Break; Sleep (1); }}
View Code

The compilation statements are:

Gcc-d_reentrant Thread4.c-o Thread4–lpthread

The result of the operation is:

$./'end'  to finish1233  characters  1234   4 characters 12345 5   3  Charactersthread joined

Second, read and write lock

Read and write locks are similar to mutexes, but read-write locks allow for higher parallelism. The mutex is either locked or unlocked, and only one thread can lock it at a time. and read-write lock has three kinds of states: Read mode lock state, write mode lock state, no lock state. Only one thread at a time can occupy write-mode read-write locks, but there can be multiple threads concurrently occupying read-mode reading and writing locks.

Although there are many different ways to implement a read-write lock, a read-write lock usually blocks subsequent read-mode lock requests if another thread attempts to write-mode locks when the read-write lock is locked, which avoids the long-term use of the reading-mode locks and waits for write-mode lock requests to remain unmet.

Read-write locks are also called shared-exclusive locks, and read-write locks are well suited for reading data structures much more frequently than write times. The interface functions for read and write locks include the following:

2.1 Creating and destroying

#include <pthread.h>intconst pthread_rwlockattr_t *restrict attr); int Pthread_rwlock_destroy (pthread_rwlock_t *rwlock);  Success returns 0, error number is returned. 

2.2 read locking and write plus lock

#include <pthread.h>int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock); int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock); int pthread_rwlock_unlock (pthread_rwlock_t * succeeds returns 0, error number is returned. )

2.3 Non-blocking access to read-write locks

#include <pthread.h>int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock); int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);  Success returns 0, error number is returned. 

Three, condition variable

A condition variable is another synchronization mechanism available to a thread. A condition variable provides a rendezvous place for multiple threads. The condition variable is used in conjunction with the mutex, allowing the thread to wait for a particular condition to occur in a non-competitive manner, which itself is protected by a mutex variable. The thread must lock the mutex before changing the condition state, and the other thread will not be aware of the change until the mutex is acquired, because it must be locked before the condition can be evaluated.

The action function for a condition variable includes the following:

3.1 Creating and destroying

#include <pthread.h>intconst pthread_condattr_t *attr); int Pthread_cond_destroy (pthread_cond_t *cond);

3.2 Items to wait

#include <pthread.h>int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); int Const struct timespec* timeout);

With pthread_cond_wait you can wait for the condition to become true, the mutex passed to the pthread_cond_wait protects the condition, the caller passes the locked mutex to the function, the function puts the calling thread on the list of threads waiting for the condition, and then unlocks the mutex. These two operations are atomic operations. This turns off the time channel between the condition check and the thread going into hibernation waiting for the condition to change, so that the thread does not miss any changes to the condition. When the pthread_cond_wait returns, the mutex is locked again.

3.3 Notification conditions

#include <pthread.h>int pthread_cond_broadcast (pthread_cond_t *cond); int pthread_cond_signal (pthread_cond_t *cond);

  These two functions can be used to inform the thread that the condition has been met, Pthread_cond_signal will wake up a thread waiting for the condition, and Pthread_cond_broadcast will wake up all the threads waiting for the condition.

Iv. Signal Volume

By the way, the semaphore defined in header file <semaphore.h> completes the encapsulation of the mutex and the conditional variable , and according to the access control mechanism in the multi-thread programming design, control the synchronization access to the resource. The provider designer is more convenient to call the interface. Its function interface is as follows:

#include <semaphore.h>int int int val); int sem_wait (sem_t *sem); int sem_post (sem_t *sem);

The semaphore is essentially a nonnegative integer counter that is used to control access to public resources. Each call to the wait operation will cause the semaphore value to be reduced by 1, and if the semaphore value is already 0, the wait operation will be blocked. Each time the post operation is called, the semaphore value is added 1.

The semaphore differs from the thread lock and condition variable in the following ways:
(1) The lock must be the same thread to get and release, otherwise it will deadlock. The condition variables and semaphores do not have to be.
(2) The signal increment and decrease will be automatically remembered by the system, there is a counter inside the system to achieve the semaphore, do not have to worry about loss, and when a condition variable is awakened, if there is no corresponding thread waiting for the condition variable, this wake will be lost.

Multithreading in Linux environments with multithreaded programming (II.)

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.