Multi-threaded things 15 (sequential locks)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

In mutually exclusive data access, there is a situation where multiple reads and writes are less. For such a situation, we also propose a read/write Lock solution. However, this lock has some defects. What are the defects? That is, This write lock can only be written after all the read locks are completed. Otherwise, the write lock needs to wait.

So, is there any way to make write operations faster? That is the sequential lock.

[CPP] View plaincopy
    1. Typedef Struct_ Sequence_lock
    2. {
    3. UnsignedIntSequence;
    4. HandleHlock;
    5. } Sequence_lock;

With such a data structure. So how can I start reading locks,

[CPP] View plaincopy
  1. UnsignedIntGet_lock_begin (sequence_lock * hseqlock)
  2. {
  3. Assert (null! = Hseqlock );
  4. ReturnHseqlock-> sequence;
  5. }
  6. IntGet_lock_retry (sequence_lock * hseqlock, unsignedIntValue)
  7. {
  8. UnsignedIntNew_value;
  9. Assert (null! = Hseqlock );
  10. New_value = hseqlock-> sequence;
  11. Return(New_value & 0x1) | (new_value ^ value );
  12. }

The write lock also needs to be modified,

[CPP] View plaincopy
  1. VoidGet_write_lock (sequence_lock * hseqlock)
  2. {
  3. Assert (null! = Hseqlock );
  4. Waitforsingleobject (hseqlock-> hlock );
  5. Hseqlock-> sequence ++;
  6. }
  7. VoidRelease_write_lock (sequence_lock * hseqlock)
  8. {
  9. Assert (null! = Hseqlock );
  10. Hseqlock-> sequence ++;
  11. Releasemutex (hseqlock-> hlock );
  12. }

If the application is used, it is not difficult,

[CPP] View plaincopy
  1. VoidRead_process (sequence_lock * hseqlock)
  2. {
  3. UnsignedIntSequence;
  4. Do{
  5. Sequence = get_lock_begin (hseqlock );
  6. /* Read operation */
  7. }While(Get_lock_retry (hseqlock, sequence ));
  8. }
  9. VoidWrite_process (sequencce_lock * hseqlock)
  10. {
  11. Get_write_lock (hseqlock );
  12. /* Write operation */
  13. Release_write_lock (hseqlock );
  14. }

Summary:
(1) There are two conditions for exit of the read lock: either the write operation is in progress or no write lock.

(2) The write locks must be mutually exclusive.

(3) The data of mutex operations cannot be pointers. Otherwise, exceptions may occur during access because it is possible to write and read simultaneously.

(4) The sequential lock cannot replace the read/write lock, because the read/write lock can ensure all data operations, but the sequential lock cannot.

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.