Concurrency Using Threads

992 views

Published on

Clone the following github repo for corresponding source code...

http://github.com/preston/concurrency_using_threads/tree/master

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
992
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Concurrency Using Threads

  1. 1. Concurrency Using Threads ...with Preston Lee. Presentation and sample code largely based on work by Jim Weirich.
  2. 2. TANSTAAFL • http://www.gotw.ca/publications/concurrency-ddj.htm • Published early 2005
  3. 3. Past Performance Gains • Clock Speed • Execution Optimization • Cache
  4. 4. Use Cases • Disk access • Application servers • Network connections • Games • Polling • Simulations • Message queuing • Scheduling • Worker processing • Long-polling
  5. 5. So, you want to write a concurrent program ...
  6. 6. Demo: race1.rb
  7. 7. What happened?
  8. 8. The Setup @amount += 1 @amount += 1 23
  9. 9. Step 1 @amount += 1 @amount += 1 23 1 23
  10. 10. Step 2 @amount += 1 @amount += 1 23 2 23 23
  11. 11. Step 3 @amount += 1 @amount += 1 24 23 3 24
  12. 12. Step 4 @amount += 1 @amount += 1 24 24 Should be 25 at this point! 24 4
  13. 13. Race Condition @amount += 1 @amount += 1 23 1 2 23 3 24 4
  14. 14. Reordering Steps @amount += 1 @amount += 1 24 1 3 25 2 25 4
  15. 15. Mutual Exclusion Do These @amount += 1 Together @amount += 1 24 1 3 25 2 25 4
  16. 16. DISABLE CONTEXT SWITCHING account.credit(1) RE-ENABLE CONTEXT SWITCHING
  17. 17. require ‘thread’ mutex = Mutex.new ... mutex.synchonize do account.credit(1) end
  18. 18. Demo: race4.rb
  19. 19. Demo: race7.rb
  20. 20. To Be Safe, You Must: (1) Protect every shared memory access with a synchronizing lock.
  21. 21. To Be Safe, You Must: (1) Protect every shared memory access with a synchronizing lock. Yes, EVERY access.
  22. 22. To Be Safe, You Must: (2) Be aware of extended situations that need to be atomic.
  23. 23. To Be Safe, You Must: (3) Have a strategy to avoid deadlock in the presence of multiple locks.
  24. 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. 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. 26. Concurrent Programming is HARD
  27. 27. And it is hard because of Shared Memory
  28. 28. What can we do to make concurrent programming easier?
  29. 29. Avoid Shared Memory
  30. 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. 31. Contact Info • Preston Lee • http://prestonlee.com • http://openrain.com • Twitter: @prestonism

×