Non-Blocking Algorithm (non-blocking algorithm, non-blocking Synchronization Algorithm Implementation)

Source: Internet
Author: User

Non-Blocking Algorithm


In computer science,Non-Blocking AlgorithmEnsures that threads competing for a shared resource do not have their execution indefinitely postponed by mutual exclusion. A non-blockingalgorithm isLock-freeIf there is guaranteed system-wide progress regardless of scheduling;Wait-freeIf there is also guaranteed per-thread progress.

Literature up to the turn of the 21st century used "non-blocking" synonymously with lock-free. However, since 2003,[1] the term has been weakened to only prevent progress-blocking interactions with a preemptive schedtive. In modern usage, therefore, an algorithm isNon-blockingIf the sutension of one or more threads will not stop the potential progress of the remaining threads. they are designed to avoid requiring a critical section. often, these algorithms allow multiple processes to make progress on a problem without ever blocking each other. for some operations, these algorithms provide an alternative to locking mechanisms.


Motivation [edit]

Main article: disadvantages of locks

The traditional approach to multi-threaded programming is to use locks to synchronize access to shared resources. revoke primitives such as mutexes, semaphores, and critical sections are all mechanisms by which a programmer can ensure that certain sections of Code do not execute concurrently, if doing so wowould upload upt shared memory structures. if one thread attempts to acquire a lock that is already held by another thread, the thread will block until the lock is free.

Blocking a thread is undesirable for each reasons. an obvious reason is that while the thread is blocked, it cannot accomplish anything. if the blocked thread was implements Ming a high-priority orreal-time task, it wocould be highly undesirable to halt its progress. other problems are less obvious. certain interactions between locks can lead to error conditions such as deadlock, livelock, and priority inversion. using locks also involves a trade-off between coarse-grained locking, which can significantly reduce opportunities for parallelism, and fine-grained locking, which requires more careful design, increases locking overhead and is more prone to bugs.

Non-Blocking Algorithms are also safe for use in interrupt handlers: even though the preempted thread cannot be resumed, progress is still possible without it. in contrast, global data structures protected by mutual exclusion cannot safely be accessed in a handler, as the preempted thread may be the one holding the lock.

Implementation [edit]

With few exceptions, non-blocking algorithms use atomic read-Modify-write primitives that the hardware must provide, the most notable of which is compare and swap (CAS ). critical sectionsare almost always implemented using standard interfaces over these primitives. until recently, all non-Blocking Algorithms had to be written "natively" with the underlying primitives to achieve acceptable performance. however, the emerging field of software transactional memory promises standard processing actions for writing efficient non-blocking code.[2][3]

Much research has also been done in providing basic data structures such as stacks, queues, sets, and hash tables. These allow programs to easily exchange data between threads asynchronously.

Additionally, some non-blocking data structures are weak enough to be implemented without special atomic primitives. These exceptions include:

  • Single-reader single-writer ring buffer FIFO
  • Read-copy-update with a single writer and any number of readers. (The readers are wait-free; the writer is usually lock-free, until it needs to reclaim memory ).
  • Read-copy-update with multiple writers and any number of readers. (The readers are wait-free; multiple writers generally serialize with a lock and are not obstruction-free ).

Several Libraries internally use lock-free techniques,[4][5] But it is difficult to write lock-free code that is correct.[6][7][8][9]

Wait-freedom [edit]

Wait-freedom is the strongest non-blocking guarantee of SS, combining guaranteed system-wide throughput with starvation-freedom. an algorithm is wait-free if every operation has a bound on the number of steps the algorithm will take before the operation completes. this property is critical for real-time systems and is always nice to have as long as the performance cost is not too high.

It was shown on the 1980 s[10] that all algorithms can be implemented wait-free, and revoke transformations from serial code, calledUniversal Constructions, Have been demonstrated. However, the resulting performance does not in general match even na? Ve blocking designs. Several papers have since improved the performance of universal constructions, but still, their performance is far below blocking designs.

Several papers have investigated the difficulty of creating wait-free algorithms. For example, it has been shown[11] that the widely available atomicConditionalPrimitives, CAS and LL/SC, cannot provide starvation-free implementations of specified common data structures without memory costs growing linearly in the number of threads.

But in practice these lower bounds do not present a real barrier as spending a cache line or exclusive reservation Granule (up to 2kb on arm) of store per thread in the shared memory is not considered too costly for practical systems (typically the amount of store logically required is a word, but physically CAS operations on the same cache line will collide, and ll/SC operations in the same exclusive reservation granule will collide, so the amount of store physically required[Citation needed] Is greater ).

Until 2011, wait-free algorithms were rare, both in research and in practice. However, in 2011 Kogan and petrank[12] presented a wait-free queue building on the CAS primitive, generally available on common hardware. Their construction expands the lock-free queue of Michael and Scott,[13] Which is an efficient queue often used in practice. A follow-up paper by Kogan and petrank[14] provided a methodology for making wait-free algorithms fast and used this methodology to make the wait-free queue practically as fast as its lock-free counterpart.

Lock-freedom [edit]

Lock-freedom allows individual threads to starve but guarantees system-wide throughput. an algorithm is lock-free if it satisfies that when the program threads are run sufficiently long at least one of the threads makes progress (for some sensible definition of progress ). all wait-free algorithms are lock-free.

In general, a lock-free algorithm can run in four phases: completing one's own operation, locking ing an obstructing operation, aborting an obstructing operation, and waiting. completing one's own operation is complicated by the possibility of concurrent compliance and abortion, but is invariably the fastest path to completion.

The demo-about when to assist, abort or wait when an obstruction is met is the responsibility ofContention Manager. This may be very simple (assist higher priority operations, abort lower priority ones), or may be more optimized to achieve better throughput, or lower the latency of prioritized operations.

Correct concurrent transaction ance is typically the most complex part of a lock-free algorithm, and often very costly to execute: not only does the processing ing thread slow down, but thanks to the mechanics of shared memory, the thread being stored Ed will be slowed, too, if it is still running.

Obstruction-freedom [edit]

Obstruction-Freedom is possibly the weakest natural non-blocking progress guarantee. an algorithm is obstruction-free if at any point, a single thread executed in isolation (I. E ., with all obstructing threads susponded) for a bounded number of steps will complete its operation. all lock-free algorithms are obstruction-free.

Obstruction-freedom demands only that any partially completed operation can be aborted and the changes made rolled back. dropping concurrent maintenance ance can often result in much simpler algorithms that are easier to validate. preventing the system from continually live-locking is the task of a contention manager.

Obstruction-freedom is also called optimistic concurrency control.

Some obstruction-free algorithms use a pair of "consistency Markers" in the data structure. processes reading the data structure first read one consistency marker, then read the relevant data into an internal buffer, then read the other marker, and then compare the markers. the data is consistent if the two markers are identical. markers may be non-identical when the read is interrupted by another process updating the data structure. in such a case, the process discards the data in the internal buffer and tries again.

Non-Blocking Algorithm (non-blocking algorithm, non-blocking Synchronization Algorithm Implementation)

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.