Linux thread synchronization (3)-read/write lock, linux thread

Source: Internet
Author: User

Linux thread synchronization (3)-read/write lock, linux thread

I. Overview

The read/write locks and mutex functions are similar to that of shared resources in the critical section! The mutex allows only one thread to enter the critical section at a time. The read/write lock has a higher concurrency than the lock. Read/write locks have the following features:

1. If a thread uses a read lock to lock the critical section, other threads can also use the read lock to enter the critical section, so that multiple threads can operate in parallel. But at this time, if you lock the write lock again, it will be blocked. After the write lock request is blocked, if there are further read lock requests, these subsequent read locks will be blocked! This prevents read locks from occupying resources for a long time and writing locks from getting hungry!

2. If a thread uses a write lock to lock the critical section, other threads will be blocked, whether it is a read lock or a write lock!

Ii. Function Interfaces

1. Create a read/write lock

1.1: Macro constant Initialization

1 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

1.2: function Initialization

1 #include <pthread.h>2 3 int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);

Rwlock: pthread_rwlock_t structure pointer of the read/write lock

Attr: the attribute structure pointer of the read/write lock. No other properties are required. The default value is NULL.

2. Lock and unlock read/write locks

1 #include <pthread.h>2 3 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);4 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);5 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

Rwlock: read/write lock pointer created

3. Other types of locks

1 #include <pthread.h>2 #include <time.h>3 4 5 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);6 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);7 8 int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);9 int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abs_timeout);

Try function lock: If the lock cannot be obtained, the error EBUSY will be returned immediately!

Timed function lock: If the lock cannot be obtained within the specified time, the ETIMEDOUT error is returned!

4. Destroy the read/write lock

1 #include <pthread.h>2 3 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

3. Simple Example

Create four threads, two threads read locks, and two threads write locks. Observe the order in which the four threads enter the critical section:

1/** 2 * @ file pthread_rwlock.c 3 */4 5 # include <stdio. h> 6 # include <stdlib. h> 7 # include <string. h> 8 # include <unistd. h> 9 # include <pthread. h> 10 11/* initialize the read/write lock */12 pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; 13/* Global Resource */14 int global_num = 10; 15 16 void err_exit (const char * err_msg) 17 {18 printf ("error: % s \ n", err_msg); 19 exit (1 ); 20} 21 22/* read lock thread function */23 void * thread_read_lock (void * Arg) 24 {25 char * pthr_name = (char *) arg; 26 27 while (1) 28 {29/* read lock */30 pthread_rwlock_rdlock (& rwlock ); 31 32 printf ("thread % s enters the critical section, global_num = % d \ n", pthr_name, global_num); 33 sleep (1 ); 34 printf ("thread % s leaves the critical section... \ n ", pthr_name); 35 36/* read unlock */37 pthread_rwlock_unlock (& rwlock); 38 39 sleep (1); 40} 41 42 return NULL; 43} 44 45/* write lock thread function */46 void * thread_write_lock (void * arg) 47 {48 char * pthr_name = (char *) Arg; 49 50 while (1) 51 {52/* write lock */53 pthread_rwlock_wrlock (& rwlock); 54 55/* write operation */56 global_num ++; 57 printf ("thread % s entering critical section, global_num = % d \ n", pthr_name, global_num); 58 sleep (1 ); 59 printf ("thread % s leaves the critical section... \ n ", pthr_name); 60 61/* write unlock */62 pthread_rwlock_unlock (& rwlock); 63 64 sleep (2); 65} 66 67 return NULL; 68} 69 70 int main (void) 71 {72 pthread_t tid_read_1, tid_read_2, tid_write_1, tid_write_2; 73 74/ * Create 4 threads, 2 reads, 2 writes */75 if (pthread_create (& tid_read_1, NULL, thread_read_lock, "read_1 ")! = 0) 76 err_exit ("create tid_read_1"); 77 78 if (pthread_create (& tid_read_2, NULL, thread_read_lock, "read_2 ")! = 0) 79 err_exit ("create tid_read_2"); 80 81 if (pthread_create (& tid_write_1, NULL, thread_write_lock, "write_1 ")! = 0) 82 err_exit ("create tid_write_1"); 83 84 if (pthread_create (& tid_write_2, NULL, thread_write_lock, "write_2 ")! = 0) 85 err_exit ("create tid_write_2"); 86 87/* wait for a thread to prevent main from ending */88 if (pthread_join (tid_read_1, NULL )! = 0) 89 err_exit ("pthread_join ()"); 90 91 return 0; 92}

Sleep (1) is used in the critical section of the two thread functions to test whether other threads can come in. 64 rows, In the write lock function, sleep (2), because the write lock request will block the next read lock, two write lock requests together will make the read lock hunger, so than 39 rows of sleep (1) one second!

Compile and run:

As you can see, read locks can enter the critical section together, and write locks in the critical section will not be available in other threads within one second !!!

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.