sources of evils in concurrency.

I found a nice entry about three important concepts in concurrent programming by Jeremy Manson.
http://jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html
atomicity, visibility and ordering…

I wanted to add some extras: side effects of optimizations and data duplications .
These two sources of evils play an important role on concurrent programing.

  • Atomicity is important for data integrity.
    When objects transit from one state to other,
    they should not expose the moment in which invariants are broken.

    girl comes in to the room and catch her boyfriend
    he was half-naked and hugging another girl.
    she got pretty upset and run away without saying anything.
    but however it was a misunderstanding,
    there wont be any problem if she was there a few seconds before or few seconds later.

    Another prominent area of the concept is Databases.
    Database programmers use transactions and transaction isolation levels,
    to achive both acceptable correctness and performance.

    This sort of problems are hard to figure out,
    but they have logical explanations in source-code-level.

  • Visibility is a hidden enemy.
    In source-code-level, there is not any reason why it happens.
    But it’s related both optimizations and hardware-design.

    There are several levels of memories.
    Fastest ones are more expensive and they are embedded into cpu.
    If data resides on the fast one, code will run faster.
    So it’s desirable, compilers get benefit of that fast memory as well as much.
    But since this special memory is very limited in size,
    most of the code will reside on slow memory.

    Parts of data will be copied to faster memory on demand,
    processed(probaby changed) and copied back to the slower one.
    This means : our data is duplicated between different kind of memories in runtime.
    ( also computers may have more than one cpu, means more duplications. )

    Altough it does not cause any problem in single-threaded applications.
    Concurrent applications are not protected by default.
    Without proper syncronization, threads may see the stale data.

  • Ordering another result of optimization.
    Compiler re-organizes the instructions into more efficient ones.
    It’s understandable, we mostly re-organize instructions in daily-life.


    mom’s shopping instructions :
    - buy 6 eggs.
    - buy 3 package of margarine.
    - buy 1 bread.
    - buy 6 eggs more.
    - leave back 2 packages of margarines, it’ too much.
    - buy 1 bread more.

    actual shopping list:
    - 2 breads (i buy them first, becos they are in the enterance.)
    - 12 eggs.
    - 1 package of margarine.

I want to suggest this great book for java developers,
i was totally blind before reading it.

http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

Leave a Reply