Precautions for developing the lock Free Structure

Source: Internet
Author: User
Yesterday I planned to rewrite the memory distributor. Previously, the memory distributor used in the system was inconvenient. I sacrificed simplicity to add the reference counting function and used some locks for thread safety. This time, all memory allocation and release are lockfree. Because memory blocks are single-chain tables, it is easier to implement lock free. It is difficult to use an array to manage idle resources. In this case, DCAS and similar methods (k word compare and k-th swap) are used ). My computer does not support CAS operations, so it cannot be determined. On a remote server, 100 threads are allocated simultaneously between 1000 and ~ The memory size ranges from 100000 to 4 bytes. The memory pool increases from 40 bytes. The lock-free structure takes about 15 seconds, and the lock structure takes about 9 seconds. Allocate 900000 to 10 threads at the same time ~ The memory size ranges from 1000000 to 4 bytes. The memory pool increases from 40 bytes. The lock-free structure takes about 14 seconds, and the lock structure takes about 14 seconds. Allocate 1800000 to five threads at the same time ~ The memory size ranges from 2000000 to 4 bytes. The memory pool increases from 40 bytes. The lock-free structure takes about 8 seconds, and the lock structure takes about 13 seconds.

The above test shows that the lock-free structure is superior to the lock structure when the number of threads approaches the actual number of kernels. However, it is ineffective when a large number of threads work. Because the memory allocation uses an array structure rather than a single-chain table structure, in theory, each operation is related to the entire array. Therefore, atomic operations on several threads are waiting for the completion of an array, A large amount of spam code is generated, which affects the execution efficiency. When the number of threads is small, the lock overhead is higher than the overhead of spam code execution.

Below is a method for DCAS to simulate using CAS:

  1. Bool CAS (volatile void ** X, void * new_x, void * old_x );
  2. Bool DCAS (volatile void ** X, void * new_x, void * old_x, volatile void ** y, void * new_y, void * old_y)
  3. {
  4. If (old_x = dcas_busy_val)
  5. Return false;
  6. If (! CAS (x, dcas_busy_val, old_x ))
  7. Return false;
  8. If (! CAS (Y, new_y, old_y ))
  9. {
  10. X = old_x;
  11. Return false;
  12. }
  13. X = new_x;
  14. Return true;
  15. }

The basic simulation idea is to use a dcas_busy_val variable to lock the X variable before updating the y variable. Then, you can safely update the y variable and then update the X variable. Other K-compare-and-K-swap methods can be implemented in this way.

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.