What is “non-blocking” concurrency and how is it different than normal concurrency?

前端 未结 8 966
滥情空心
滥情空心 2021-01-30 02:26
  1. What is \"non-blocking\" concurrency and how is it different than normal concurrency using threads? Why don\'t we use non-blocking concurrency in all the scenarios where
8条回答
  •  时光说笑
    2021-01-30 02:57

    Non-blocking synchronization is the same a blocking synchronization, both are kind of synchronization, the only difference is that non-blocking synchronization is faster overall.

    For starters you want to use synchronization only when multiple threads access the same resource in RAM. You can't use synchronization when trying to access things on disk, or better said, you need to use locks on disk.

    That said, how can you synchronize if no thread ever blocks?

    The answer is optimistic locking. This idea has existed for at least 20 years. Maybe more.

    You maybe have heard of the Lisp language. As it turns out functional languages never modify its parameters, only return new values, so they never need to synchronize.

    In Lisp you can have shared state, but it gets tricky. So most programs can run in parallel and never worry about synchronization.

    The idea of optimistic locking is that all threads modify shared valued willingly, but they have a local area to modify the values, and only apply the modification at the end, with one instruction, atomically, using CAS. Cas stands for Compare And Swap,it performs in just one CPU cycle, and it has been implemented in CPUs for at least 20 years.

    An excellent explanation of what CAS is: https://www.cs.umd.edu/class/fall2010/cmsc433/lectures/nonBlocking.pdf

    So if there is a conflict in the modification, it will only affect one of the writers, the rest will be done with it.

    Moreover, if there is no contention whatsoever, non-blocking algorithms perform a lot faster than their blocking counterparts.

    Tutorial on non-blocking algorithms in Java with code examples you can use in real life: http://tutorials.jenkov.com/java-concurrency/non-blocking-algorithms.html

提交回复
热议问题