Study Notes in Windows via C/C ++-"read/write lock" for "thread synchronization" in user mode"

Source: Internet
Author: User

The read/write lock, slim reader-writer locks (srwlock), is also a method for user thread synchronization.

A typical thread synchronization problem is called the reader-Writer Problem ". When a writer is "writing" some resources, no other reader or writer can obtain these resources. When a reader is "reading" some resources, other readers are allowed to "read" together, but no writer is allowed to "write" these resources. Here, "write" means "modify", and "read" means "Use Only without modification ".

In other words, "multiple readers can share resources, and a single writer exclusively occupies resources ".

This mechanism requires that any reader or writer must lock the resource before accessing the resource, release the lock when you no longer need to access the resource.

Therefore, there is a "read/write lock" mechanism. Another method is called exclusive lock and shared lock ". How does this mechanism stipulate:

1. Reader-oriented: when a "Reader" wants to access a resource, it first adds a "read lock" (shared lock) to the resource. If the lock is successful, the resource can be accessed.

2. Take the writer as the subject: when a "Writer" wants to access the resource, it must add a "Write lock" (exclusive lock) to the resource before the resource can be accessed.

3. Use a resource as the body: If a write lock (exclusive lock) is added to this resource, no other write lock or read lock (shared lock) can be added to this resource, this ensures that the writer is not damaged by resources when modifying the resource. When a read lock is added to the resource, the reader can only continue to lock the resource, the write lock is not allowed to be added to this resource, so that when "at least" a reader is using this resource, the resource will not be modified and destroyed by the writer.

  

We regard "Reader" and "Writer" as two types of threads, so that we can use the read/write lock method to handle thread synchronization issues. Note that (in the previous Windows Vista system, there was no api related to the read/write lock mechanism, so the API in this article is not applicable to Windows systems earlier than Vista ).

 

First, define and assign a srwlock structure, initialize it, call the function: initializesrwlock, and pass a pointer to the structure.

Void initializesrwlock (psrwlock srwlock );

 

This function sets the internal data of the srwlock structure internally. The members in this structure should be transparent to you and you should not set it yourself.

Once a srwlock is initialized, a writer thread can request an exclusive lock to access protected resources. Call the function: acquiresrwlockexclusize to pass the address of an initialized srwlock structure.

Void acquiresrwlockexclusive (psrwlock srwlock );

 

When the writer thread completes resource modification, you can use the releasesrwlockexclusive function to release the exclusive lock for access requests. Transmits the address of the srwlock structure used during the request. Void releasesrwlockexclusive (psrwlock srwlock );

 

Relatively, for "Reader thread", you can use the following two functions to request access (adding a shared lock to the resource) and release a shared lock.

Void acquiresrwlockshared (psrwlock srwlock ); Void releasesrwlockshared (psrwlock srwlock );

 

Unlike key code segments, for read/write locks, there is no need to request access for a resource multiple times in a thread. Because the same exclusive lock cannot be added multiple times, once the shared lock is added, there is no need to add it. Therefore, you do not need to request a resource lock Multiple times or release the same lock multiple times.

In addition, when a read/write lock is used, only the initializeswrlock function that initializes the read/write lock does not have a function that deletes or destroys the read/write lock. As for the function that destroys or deletes the read/write lock, this is automatically maintained by the system.

 

This section also compares the performance of thread synchronization machines in various user modes, and does not need to be detailed as long as you know the results.

From the running speed point of view: mutual lock function> read/write lock ≥ key code segment> mutex Kernel Object

In the above case, "=" indicates the result in a single thread.

It can be seen that the thread synchronization efficiency in user mode is higher than that in kernel mode (the mutex Kernel Object runs in kernel mode ). The efficiency of read/write locks is higher than that of key code segments, because when the key code segments compete, an event Kernel Object (running in kernel mode) is created inside the key code segment ), in addition, the read locks in read/write locks enable resource sharing. The efficiency in multithreading is higher than that in key code segments with exclusive resources.

Therefore, we recommend that you use a read/write lock instead of a key code segment.

Note that Windows systems earlier than Vista do not support read/write locks, so these functions cannot be used in Windows systems earlier than Vista. Key code segments are recommended in these systems.

 

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.