Linux read/write locks

Source: Internet
Author: User
/* Function, Linux read/write lock blog, http://blog.csdn.net/shunqiziranhao007/article/details/8700288date, April 1, March 21, 2013, including <stdio. h> # include <pthread. h> # include <unistd. h> // only one thread can lock the mutex. Therefore, the concurrency is not high. pthread_mutex_t mutex = pthread_mutex_initializer; int gcount1 = 0; void * TF1 (void * P) {pthread_mutex_lock (& mutex); ++ gcount1; printf ("TF1 write, gcount1 is % d \ n", gcount1); // After obtaining mutex, wait 1 s to see if other mutex threads will wait 1 ssleep (1); pthread_mut Ex_unlock (& mutex); Return 0;} void * TF2 (void * P) {pthread_mutex_lock (& mutex); printf ("TF2 read, gcount1 is % d \ n ", gcount1); // wait 1 s after obtaining mutex to check whether other mutex threads will wait 1 ssleep (1); pthread_mutex_unlock (& mutex); Return 0 ;} void * tf3 (void * P) {pthread_mutex_lock (& mutex); printf ("tf3 read, gcount1 is % d \ n", gcount1); // After obtaining mutex, wait 1 s to see if other mutex threads will wait 1 ssleep (1); pthread_mutex_unlock (& mutex); Return 0 ;}// check the effect of using the read/write lock. There is only one A thread occupies the read/write lock in the write mode, but multiple threads can simultaneously occupy the read/write lock in the Read mode. Pthread_rwlock_t lock; int gcount2; void * tf4 (void * P) {pthread_rwlock_wrlock (& lock); ++ gcount2; printf ("tf4 write, gcount2 is % d \ n ", gcount2); // wait 1 s after getting rwlock to check whether other threads that require rwlock will wait 1 ssleep (1); pthread_rwlock_unlock (& lock); Return 0 ;} void * tf5 (void * P) {pthread_rwlock_rdlock (& lock); printf ("tf5 read, gcount2 is % d \ n", gcount2); // After obtaining rwlock, wait 1 s to see if other threads that require rwlock will wait 1 ssleep (1); pthread_rwlock_unlock (& lock); Return 0;} void * tf6 (void * P) {pthread_rwlock_rdlock (& lock); printf ("tf6 read, gcount2 is % d \ n", gcount2 ); // after getting rwlock, wait 1 s to see if other threads that need rwlock will wait 1 ssleep (1); pthread_rwlock_unlock (& lock); Return 0 ;} void * tf7 (void * P) {pthread_rwlock_wrlock (& lock); ++ gcount2; printf ("tf7 write, gcount2 is % d \ n", gcount2 ); // after getting rwlock, wait 1 s to see if other threads that need rwlock will wait 1 ssleep (1); pthread_rwlock_unlock (& lock); Return 0 ;} int main () {pthread_t tid1, Tid2, tid3, tid4, tid5, tid6, tid7; pthread_create (& tid1, 0, TF1, 0); pthread_create (& tid2, 0, TF2, 0 ); pthread_create (& tid3, 0, tf3, 0); pthread_join (tid1, 0); pthread_join (tid2, 0); pthread_join (tid3, 0 ); // from top to bottom // only one thread can lock mutex at a time and wait for other requests. This is equivalent to serial execution of pthread_rwlock_init (& lock, 0); pthread_create (& tid4, 0, tf4, 0); pthread_create (& tid5, 0, tf5, 0); pthread_create (& tid6, 0, tf6, 0); pthread_create (& tid7, 0, Tf7, 0); pthread_join (tid4, 0); pthread_join (tid5, 0); pthread_join (tid6, 0); pthread_join (tid7, 0); pthread_rwlock_destroy (& lock ); // The write lock can only be executed by one thread while other threads wait. // Request the read/write lock at the same time. The thread requesting the write operation first obtains the lock. // Multiple threads can simultaneously obtain the read lock return 0;}/* $. /rwlock.exe TF1 write, gcount1 is 1tf2 read, gcount1 is 1tf3 read, gcount1 is 1tf4 write, gcount2 is 1tf7 write, gcount2 is 2tf6 read, gcount2 is 2tf5 read, gcount2 is 2 */

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.