Concurrency Using Threads

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Concurrency Using Threads - Presentation Transcript

    1. Concurrency Using Threads ...with Preston Lee. Presentation and sample code largely based on work by Jim Weirich.
    2. TANSTAAFL • http://www.gotw.ca/publications/concurrency-ddj.htm • Published early 2005
    3. Past Performance Gains • Clock Speed • Execution Optimization • Cache
    4. Use Cases • Disk access • Application servers • Network connections • Games • Polling • Simulations • Message queuing • Scheduling • Worker processing • Long-polling
    5. So, you want to write a concurrent program ...
    6. Demo: race1.rb
    7. What happened?
    8. The Setup @amount += 1 @amount += 1 23
    9. Step 1 @amount += 1 @amount += 1 23 1 23
    10. Step 2 @amount += 1 @amount += 1 23 2 23 23
    11. Step 3 @amount += 1 @amount += 1 24 23 3 24
    12. Step 4 @amount += 1 @amount += 1 24 24 Should be 25 at this point! 24 4
    13. Race Condition @amount += 1 @amount += 1 23 1 2 23 3 24 4
    14. Reordering Steps @amount += 1 @amount += 1 24 1 3 25 2 25 4
    15. Mutual Exclusion Do These @amount += 1 Together @amount += 1 24 1 3 25 2 25 4
    16. DISABLE CONTEXT SWITCHING account.credit(1) RE-ENABLE CONTEXT SWITCHING
    17. require ‘thread’ mutex = Mutex.new ... mutex.synchonize do account.credit(1) end
    18. Demo: race4.rb
    19. Demo: race7.rb
    20. To Be Safe, You Must: (1) Protect every shared memory access with a synchronizing lock.
    21. To Be Safe, You Must: (1) Protect every shared memory access with a synchronizing lock. Yes, EVERY access.
    22. To Be Safe, You Must: (2) Be aware of extended situations that need to be atomic.
    23. To Be Safe, You Must: (3) Have a strategy to avoid deadlock in the presence of multiple locks.
    24. To Be Safe, You Must: (4) Evaluate every single library used by your program to see if they also follow rules 1 - 3.
    25. Double Checked Lock public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { //1 if (instance == null) //2 instance = new Singleton(); //3 } } return instance; } The theory behind double-checked locking is perfect. Unfortunately, reality is entirely different.
    26. Concurrent Programming is HARD
    27. And it is hard because of Shared Memory
    28. What can we do to make concurrent programming easier?
    29. Avoid Shared Memory
    30. Are We Blug Programmers? ... Languages less powerful than Blub are obviously less powerful, because they're missing some feature he's used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages... Blub is good enough for him, because he thinks in Blub. -- Paul Graham, Beating the Averages
    31. Contact Info • Preston Lee • http://prestonlee.com • http://openrain.com • Twitter: @prestonism

    + Preston LeePreston Lee, 7 months ago

    custom

    393 views, 0 favs, 0 embeds more stats

    Clone the following github repo for corresponding s more

    More info about this presentation

    © All Rights Reserved

    • Total Views 393
      • 393 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 13
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories