Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement read-write Lock rwlock by Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Linux how to achieve read-write lock rwlock", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux how to achieve read-write lock rwlock" this article.

Read-write locks are another way to achieve synchronization between threads. Similar to mutexes, read-write locks divide operations into read and write modes, and multiple threads can occupy read-write locks at the same time, which makes read-write locks have higher parallelism.

The characteristics of read-write lock are: write monopoly, read sharing, and high priority of write lock. For read-write locks, mastering these 12 words is enough.

In a Linux environment, read-write locks have the following three states:

Locked state in read mode (read lock)

Locked state in write mode (write lock)

Unlocked state

Although the read-write lock has three states: read lock, write lock and unlocked state, it actually has only one lock, not three.

As mentioned earlier, the characteristics of read-write lock are: write monopoly, read sharing, and high priority of write lock. Specifically:

When a read-write lock is locked in write mode, all threads that attempt to lock the lock (whether read or write) will be blocked before unlocking;-- > write exclusive

When a read-write lock is locked in read mode, the thread will succeed if it is locked in read mode; if it is locked in write mode, it will block. -- > read sharing

When a read-write lock is locked in read mode, there are both threads trying to lock in write mode and threads trying to lock in read mode. Then the read-write lock blocks subsequent read-mode lock requests and gives priority to the write-mode lock. -- > write lock has high priority

Read-write locks are also called shared-exclusive locks. When a read-write 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 monopoly, read sharing.

Read-write locks are ideal for situations where data structures are read more often than they are written. Because the read lock is shared, this can improve parallelism.

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 values of the above seven functions are: 0 for success, and error number for failure.

Pthread_rwlock_t type: used to define a read-write lock variable, such as: pthread_rwlock_t rwlock

# # pthread_rwlock_init function

Function prototype:

Int pthread_rwlock_init (pthread_rwlock_t restrict rwlock, const pthread_rwlockattr_t restrict attr)

Function: initialize a read-write lock

Parameter description:

Rwlock: outgoing parameter, which should be passed & rwlock to the function when called

Attr: indicates the read-write lock attribute. NULL is usually passed, which means the default attribute is used.

# # pthread_rwlock_destroy function

Function prototype:

Int pthread_rwlock_destroy (pthread_rwlock_t * rwlock)

Function: destroy a read-write lock

# # pthread_rwlock_rdlock function

Function prototype:

Int pthread_rwlock_rdlock (pthread_rwlock_t * rwlock)

Function: request a read-write lock by reading. (often abbreviated as: request read lock)

# # pthread_rwlock_wrlock function

Function prototype:

Int pthread_rwlock_wrlock (pthread_rwlock_t * rwlock)

Function: request a read-write lock in a write manner. (often abbreviated as: request write lock)

# # pthread_rwlock_unlock function

Function prototype:

Int pthread_rwlock_unlock (pthread_rwlock_t * rwlock)

Function function: unlock.

# # pthread_rwlock_tryrdlock function

Function prototype:

Int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock)

Function function: non-blocking request read-write lock (non-blocking request read lock)

# # pthread_rwlock_trywrlock function

Function prototype:

Int pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock)

Function function: non-blocking request read-write lock (non-blocking request write lock)

The above is all the content of the article "how Linux implements read-write lock rwlock". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report