[Linux operating system] Read-write locks in Linux system programming


1. The principle of read-write lock

Read-Write Lock (Read-Write Lock) is a special lock mechanism for reading and writing shared resources in a multi-threaded environment. Unlike mutexes, read-write locks allow multiple threads to read a shared resource at the same time, but only allow one thread to write. Read-write locks can improve concurrency performance and are suitable for scenarios where read operations are more frequent than write operations.


2. Provided operation function

In Linux system programming, the related functions of the read-write lock are also provided through the pthread library. Commonly used read-write lock related functions are:

  1. pthread_rwlock_initThe role of the function is to initialize the read-write lock.
    • prototype:int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
    • parameter:
      • rwlock: Pointer to the read-write lock variable to be initialized.
      • attr: Pointer to the read-write lock attribute, usually set to NULL.
    • Return value: Returns 0 on success, returns an error code on failure.

  1. pthread_rwlock_destroyThe function of the function is to destroy the read-write lock.
    • prototype:int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
    • parameter:
      • rwlock: Pointer to the read-write lock variable to be destroyed.
    • Return value: Returns 0 on success, returns an error code on failure.

  1. pthread_rwlock_rdlockThe role of the function is to acquire a read lock.
    • prototype:int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
    • parameter:
      • rwlock: Pointer to the read-write lock variable to acquire the read lock.
    • Return value: Returns 0 on success, returns an error code on failure.

  1. pthread_rwlock_wrlockThe role of the function is to acquire a write lock.
    • prototype:int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
    • parameter:
      • rwlock: Pointer to the read-write lock variable to acquire the write lock.
    • Return value: Returns 0 on success, returns an error code on failure.

  1. pthread_rwlock_unlockThe role of the function is to release the read lock or write lock.
    • prototype:int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
    • parameter:
      • rwlock: Pointer to the read-write lock variable to be released.
    • Return value: Returns 0 on success, returns an error code on failure.

sample code

#include <stdio.h>
#include <pthread.h>

pthread_rwlock_t rwlock;
int shared_data = 0;

void* reader_func(void* arg) {
    
    
    pthread_rwlock_rdlock(&rwlock);
    printf("Reader %ld read shared data: %d\n", (long)arg, shared_data);
    pthread_rwlock_unlock(&rwlock);
    return NULL;
}

void* writer_func(void* arg) {
    
    
    pthread_rwlock_wrlock(&rwlock);
    shared_data += 1;
    printf("Writer %ld wrote shared data: %d\n", (long)arg, shared_data);
    pthread_rwlock_unlock(&rwlock);
    return NULL;
}

int main() {
    
    
    pthread_rwlock_init(&rwlock, NULL);

    pthread_t reader_thread1, reader_thread2, writer_thread;
    pthread_create(&reader_thread1, NULL, reader_func, (void*)1);
    pthread_create(&reader_thread2, NULL, reader_func, (void*)2);
    pthread_create(&writer_thread, NULL, writer_func, (void*)1);

    pthread_join(reader_thread1, NULL);
    pthread_join(reader_thread2, NULL);
    pthread_join(writer_thread, NULL);

    pthread_rwlock_destroy(&rwlock);

    return 0;
}

4. Example explanation

In the above code, we use pthread_rwlock_initthe function to initialize a read-write lock, and then create two read threads and one write thread. The reading thread pthread_rwlock_rdlockobtains the read lock through the function, and the writing thread obtains the write lock through pthread_rwlock_wrlockthe function, and then performs corresponding read and write operations. Finally, pthread_rwlock_unlockthe lock is released through the function, and pthread_rwlock_destroythe read-write lock is destroyed through the function.


insert image description here

Guess you like

Origin blog.csdn.net/Goforyouqp/article/details/132412097