Concurrent programming - parallel level

When looking multicore programming related papers, often a concurrent algorithms would say that they are wait-free or lock-free , or non-blocking , and these technical terms in fact represents the degree of concurrency, or the level of concurrency . Concurrency level of understanding is an essential basis for reading a variety of concurrent algorithm design papers, and concurrent data structures to achieve.

1.1 Wait-freedom without waiting for concurrency

Wait-freedom means that each thread will run forever without waiting for external conditions, the entire process can be completed in any operation within a limited step, which is the highest level of concurrency, without any obstruction.

 Combined with knowledge of the operating part of the atom before, you can simply thought to directly call the algorithm or program an atomic operation implemented belongs to the Wait-as Free , as in the following increment_reference_counter function is wait-free , and it encapsulates atomic_increment the atomic increment primitives, multiple threads can call this function at the same time on the same memory variable increment, without any obstruction (there is also a blockage, a bus lock level)

 Make this comparison, CAS ( Compare and the swap, CAS operation includes three operands - a memory location ( V ), the pre- stage original value ( A ) and the new value (B) If the memory position of the original value with the expected value. match, then the processor will automatically update the location value to the new value. otherwise, the processor does nothing. in either case, it will be in the CAS returns the value of the position before the command. (in CAS in some special cases will return only the CAS is successful, without extracting the current value. ) call the class is not a wait-free . Note wait-free primitives can not contain the inner loop, CAS primitive when used normally included in the " cycle until it succeeds ." the inner loop.

 void increment_reference_counter(rc_base* obj)

{

    atomic_increment(obj->rc);

}

1.2 Lock-freedom lock-free concurrency

Lock-freedom refers to the entire system as a whole has been run down, the internal systems may be a single thread starvation within a certain period of time, which is more than wait-freedom weak level of concurrency, but the overall system point of view is still no obstruction. All wait-free algorithms are clearly meet the lock-free requirements.

 Lock-free algorithms usually through synchronization primitives CAS implementation.

 void stack_push(stack* s, node* n)

{

    node* head;

    do

    {

        head = s->head;

        n->next = head;

    }

    while ( ! atomic_compare_exchange(s->head, head, n));

}

Multiple threads call the above function, in theory, a thread may have been trapped inside the loop, but once there is a thread atomic operation fails and returns cycle, means that there are other threads successful execution of the atomic operation and exit the loop, so as to ensure the overall system there is no obstruction.

 In fact, in front of the atomic increment function can also use the following primitives to achieve, in this implementation, the threads are no longer all non-blocking, and some threads may be because the CAS failure wrap several cycles.

void increment_reference_counter(rc_base* obj)

{

       Int rc;

       Do {

       rc = obj-> rc;

} while(!atomic_compare_exchange(obj->rc,rc,rc+1));

}

1.3 Obstruction-freedom non-blocking concurrent

Obstruction-free means that at any point in time, each operation may be an isolated thread ends run in limited steps. As long as there is no competition, the thread can run continuously once shared data is modified, Obstruction as Free- required suspension of part of the operation has been completed, and rollback, obstruction-as Free is a lower level of concurrency concurrent non-blocking, the algorithm does not appear in provide single-threaded execution-style progress to ensure that the case of conflict of operation, all the Lock-Free algorithms are Obstruction-free is.

1.4 Blocking algoithms blocking concurrent

Blocking algorithm class is the lowest level of concurrency synchronization algorithm, it is generally necessary to generate obstruction. It can be realized simply that the lock is based blocking algorithm. Details refer to Chapter 5

Several may be used above the level of concurrency FIG description:

Blue is blocking algorithm, green is non-blocking algorithm, the higher the top of the pyramid, the concurrency level, the better the performance, the pyramid is the right tool to achieve (atomic, lock, mutex, etc.)

 

Published 47 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/nanchengyu/article/details/53063437