Linux: Synchronizing threads with read-write locks

Source: Internet
Author: User
Tags mutex

Basic and Control Primitives

Read/write Lock

Similar to mutex, but read-write locks allow for higher parallelism. Its characteristics are: Write exclusive, read share.

Read-Write lock status:

A read-write lock has three states:

1. Lock status in read mode (read lock)

2. Lock status in write mode (write lock)

3. No lock status

Read/write lock characteristics:

    1. When a read-write lock is "write mode plus lock", all threads that lock on the lock are blocked until the lock is unlocked.
    2. Read-mode lock is a success if the thread locks it in read mode, or if the thread locks in write mode.
    3. Read-write locks are both threads that attempt to lock in write mode and threads that attempt to lock in read mode. Then the read-write lock blocks subsequent reads-mode lock requests. The Write mode lock is preferred. read-Lock, write-lock parallel blocking, high write-lock priority

Read-write locks are also called shared-exclusive locks. When a read-write lock is locked in reading mode, it is locked in shared mode, and it is locked in exclusive mode when it is locked in write mode. write exclusive, read share.

Read-write locks are well suited for reading data structures much more often than written.

Main application functions:

Pthread_rwlock_init function

Pthread_rwlock_destroy function

Pthread_rwlock_rdlock function

Pthread_rwlock_wrlock function

Pthread_rwlock_tryrdlock function

Pthread_rwlock_trywrlock function

Pthread_rwlock_unlock function

The return value of the above 7 functions is: Successful return 0, failure directly returns the error number.

The pthread_rwlock_t type is used to define a read-write lock variable.

pthread_rwlock_t Rwlock;

Pthread_rwlock_init function

Initialize a read/write lock

int Pthread_rwlock_init (pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);

The 2:attr table reads and writes the lock property, usually using the default property, passing null.

Pthread_rwlock_destroy function

Destroy a read-write lock

int Pthread_rwlock_destroy (pthread_rwlock_t *rwlock);

Pthread_rwlock_rdlock function

Read-write locks are requested in reading mode. (often referred to as: request read lock)

int Pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);

Pthread_rwlock_wrlock function

Request a read-write lock in write mode. (often referred to as: Request write lock)

int Pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);

Pthread_rwlock_unlock function

Unlock

int Pthread_rwlock_unlock (pthread_rwlock_t *rwlock);

Pthread_rwlock_tryrdlock function

Non-blocking read-write lock request (non-blocking request read lock)

int Pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);

Pthread_rwlock_trywrlock function

Non-blocking write request read-write lock (non-blocking request write lock)

int Pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock);

Read-Write Lock example

See the example below, with multiple threads reading and writing to the same global data.

#include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

#include<unistd.h>

pthread_rwlock_t Rwlock;

Longint love ;

void *PTH_WR (void *arg)

{

int i = (int)arg;

while (Love <= 520)

{

Pthread_rwlock_wrlock (&rwlock); //Request write lock

printf ("write================ global variable love =%ld, I'm%d line.") \ n ", love + = +, i + 1);

Pthread_rwlock_unlock (&rwlock); //unlock

Sleep (1);

}

returnNULL;

}

void *pth_rd (void *arg)

{

int i = (int)arg;

while (Love <= 520)

{

Pthread_rwlock_rdlock (&rwlock); //Request read lock

printf ("global variable" love =%ld, I am%d line.) —————-read\n ", Love, i + 1);

Pthread_rwlock_unlock (&rwlock); //unlock

Sleep (1);

}

returnNULL;

}

int main (void)

{

pthread_t pth[10];

int i;

Pthread_rwlock_init (&rwlock, NULL);

for (i = 0; I! = 5; i++)/ /write

{

Pthread_create (&pth[i], NULL, PTH_WR, (void *) i);

}

for (i = 0; I! = 5; i++)/ /Read

{

Pthread_create (&pth[5 + i], NULL, pth_rd, (void *) i);

}

while (1)

{

if (Love >= 520)

{

for (int j = 0; J! = Ten; J + +)

{

Pthread_cancel (Pth[j]); //Kill thread

Pthread_join (Pth[j], NULL); Recycle thread

}

break;

}

}

return 0;

}

Inside, a global variable is defined love, which stops reading and writing and kills the Recycle thread When love is greater than 520. Read and write lock unlocked position and the same as the mutex to unlock the location of the same, look directly at the result: see, read the data when the same, and read the data is consistent with the data written in the previous, and did not appear to read the data and write inconsistent data. Although the order of thread execution is not sequential, this is not important, it is just the result of kernel scheduling and thread contention for resources, and we are not concerned with the order of thread execution, but rather the result. Obviously, after the lock is neat, we want the effect, of course, this is written on the 64-bit compiler will have a warning: the reason I said on the previous blog, here no longer explain.

Now let's look at the case of no lock, the code is to remove those locks is.

This is the result of adding 40 each time;

This is the result of adding 20 each time, you can see that the more the number of times, the more chaotic situation is more serious.

Linux: Synchronizing threads with read-write locks

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.