linux_thread read-write lock_pthread_rwlock_rdlock function_pthread_rwlock_wrlock function_pthread_rwlock_tryrdlock function_pthread_rwloc

Continued from the previous article: linux_thread lock mutex (mutex)_thread synchronization_deadlock phenomenon_pthread_mutex_lock function_pthread_mutex_unlock function_deadlock phenomenon

  Today I will share with you another lock in linux: a read-write lock, which is similar to a mutex lock, but a read-write lock allows higher parallelism. Its characteristics are: write exclusive, read shared. It is mainly to share some application functions of read-write locks, and start serving:

The catalog of articles published by this blogger on CSDN: My CSDN catalog, as a guide to the types of articles published by bloggers on CSDN

1. Read-write lock status:

A read-write lock has three states:
  1. Locked state in read mode (read lock)
  2. Locked state in write mode (write lock)
  3. Unlocked state

2. Read-write lock features:

1、读写锁是“写模式加锁”时, 解锁前,所有对该锁加锁的线程都会被阻塞。
2、读写锁是“读模式加锁”时, 如果线程以读模式对其加锁会成功;如果线程以写模式加锁会阻塞。
3、读写锁是“读模式加锁”时, 既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写锁会阻塞随后的读模式锁请求。优先满足写模式锁。读锁、写锁并行阻塞,写锁优先级高

  Read-write locks are also called shared-exclusive locks. When a reader-writer lock is locked in read mode, it is locked in shared mode; when it is locked in write mode, it is locked in exclusive mode. Write exclusive, read shared.
  Read-write locks are very suitable for situations where the number of reads to the data structure is much greater than that of writes.

3. Main application functions:

  The pthread_rwlock_t type is used to define a read-write lock variable.
  pthread_rwlock_t rwlock;
  All the return values ​​of this series of functions, when the return value is not 0, can print error information through the strerror function.

3.1.pthread_rwlock_init function

Function:
  Initialize a read-write lock.
Header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
Function parameters:
  rwlock: Read-write lock that needs to be initialized.
  attr: Read-write lock attribute, usually use the default attribute, just pass NULL.
Return value:
  success: return 0;
  failure: return non-zero.

3.2.pthread_rwlock_destroy function

Function:
  Destroy a read-write lock.
Header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
Function parameters:
  rwlock: Read-write lock that needs to be initialized.
Return value:
  success: return 0;
  failure: return non-zero.

3.3.pthread_rwlock_rdlock function

Function:
  Request a read-write lock in read mode. (Often abbreviated as: request read lock)
Header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
Function parameters:
  rwlock: The read-write lock that needs to be requested.
Return value:
  success: return 0;
  failure: return non-zero.

3.4.pthread_rwlock_wrlock function

Function:
  Request a read-write lock in writing mode. (Often abbreviated as: request write lock)
Header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
Function parameters:
  rwlock: The read-write lock that needs to be requested.
Return value:
  success: return 0;
  failure: return non-zero.

3.5.pthread_rwlock_unlock function

Function function:
  Unlock
header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
Function parameters:
  rwlock: the lock to be unlocked.
Return value:
  success: return 0;
  failure: return non-zero.

3.6.pthread_rwlock_tryrdlock function

Function:
  non-blocking request for read-write lock in read mode (non-blocking request for read-write lock)
header file:
  #include <pthread.h>
function prototype:
  int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
function parameter:
  rwlock: read and write that needs to be requested Lock.
Return value:
  success: return 0;
  failure: return non-zero.

3.7.pthread_rwlock_trywrlock function

Function function:
  Non-blocking request for read-write lock in writing mode (non-blocking request for write lock)
Header file:
  #include <pthread.h>
Function prototype:
  int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
Function parameters:
  rwlock: read and write that needs to be requested Lock.
Return value:
  success: return 0;
  failure: return non-zero.

4. Example of read-write lock

See the following example, where multiple threads read and write the same global data at the same time.


//3个线程不定时 "写" 全局资源,5个线程不定时 "读" 同一全局资源
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int counter; //全局变量
pthread_rwlock_t rwlock;//读写锁
//写线程 回调函数
void *th_write(void *arg)
{
    
    
    int t;
    int i = (int)arg;
    while (1) {
    
    
        t = counter;
        usleep(1000);//睡眠1000微妙
        pthread_rwlock_wrlock(&rwlock);//加写锁
        printf("=======write %d: %lu: counter=%d ++counter=%d\n", i, pthread_self(), t, ++counter);
        pthread_rwlock_unlock(&rwlock);//解锁
        usleep(5000);//睡眠5000微妙
    }
    return NULL;
}
//读线程回调函数
void *th_read(void *arg)
{
    
    
    int i = (int)arg;
while (1)
 {
    
    
        pthread_rwlock_rdlock(&rwlock);//加读锁
        printf("----------------------------read %d: %lu: %d\n", i, pthread_self(), counter);
        pthread_rwlock_unlock(&rwlock);//解锁
        usleep(900);//睡眠900微妙
    }
    return NULL;
}
//主函数
int main(void)
{
    
    
    int i;
    pthread_t tid[8];
    pthread_rwlock_init(&rwlock, NULL);//初始化读写锁
	//创建3个写线程
    for (i = 0; i < 3; i++)
        pthread_create(&tid[i], NULL, th_write, (void *)i);
	//创建5个读线程
    for (i = 0; i < 5; i++)
        pthread_create(&tid[i+3], NULL, th_read, (void *)i);
	//等待线程退出
    for (i = 0; i < 8; i++)
        pthread_join(tid[i], NULL);
	//释放读写锁
    pthread_rwlock_destroy(&rwlock);            //释放读写琐
    return 0;
}

  The above is the sharing of this time, I hope it will be helpful to everyone, welcome to follow the blogger to learn more new knowledge together!

Guess you like

Origin blog.csdn.net/qq_44177918/article/details/130461090