Thread Blocking is Evil

1,046 views

Published on

This is the simple version of the orinial one addressed at Deview2011.

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

  • Be the first to like this

No Downloads
Views
Total views
1,046
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Thread Blocking is Evil

  1. 1. Thread Blocking Is EvilGame Network Component Team Kim, J.H.
  2. 2. CONCURRENCY
  3. 3. Why isThread Blocking a Concern?
  4. 4. “The Free Lunch is OVER.” Herb Sutter December 2004
  5. 5. Clock speedExecution optimizationCache
  6. 6. HyperthreadingMulticoreCache
  7. 7. Write Multithreaded Application
  8. 8. CONCURRENCY
  9. 9. Waiting in Line
  10. 10. ThreadBlocking
  11. 11. Starvation Deadlock
  12. 12. Starvation
  13. 13. Task A Task B1: Lock 1: Sleep 0 msec2: Sleep 1msec3: Unlock
  14. 14. 1000Process time(ms) 995 Task B 990 Task A 985 1 2 3 4 5 6 7 8 9 # of Thread
  15. 15. Deadlock Hold & Wait
  16. 16. Thread A Thread B1A: Lock 1B: Lock2A: Request 2B: Send Response3A: Wait for Response 3B: Unlock4A: Do something with Response5A: Unlock
  17. 17. Thread A Thread B1A: Lock 1B: Lock2A: Request 2B: Send Response3A: Wait for Response 3B: Unlock4A: Do something with Response5A: Unlock
  18. 18. MyProblem
  19. 19. Framework forLobby-Room style Game
  20. 20. Remote Procedure Call
  21. 21. Easy to write code because it’s allcontained inside one function.
  22. 22. ‘Enter Room’ RPC ‘Authentication’ RPCGame Client Room Lobby
  23. 23. Game Client Room Lobby Call ‘Enter Room’ RPC Call ‘Authentication’ RPC ‘Authentication ‘Enter Room’ RPC ‘ RPC function Function body body Return ‘Authentication’ RPC Return ‘Enter Room’ RPC
  24. 24. Game Client Room Lobby Call ‘Enter Room’ RPC Call ‘Authentication’ RPC ‘Enter Room’ ‘Authentication RPC function RPC ‘ body Function body Return ‘Authentication’ RPC Return ‘Enter Room’ RPC
  25. 25. Game Client Room Lobby Response Delay Call ‘Enter Room’ RPC Call ‘Authentication’ RPC From Lobby Server ‘Enter Room’ ‘Authentication RPC function RPC ‘ body Function body Return ‘Authentication’ RPC Return ‘Enter Room’ RPC
  26. 26. Game Client Room Lobby Response Delay Call ‘Enter Room’ RPC Call ‘Authentication’ RPC From Lobby Server ‘Enter Room’ ‘Authentication RPC function RPC ‘ body Function body Room Server Return ‘Enter Room’ RPC Return ‘Authentication’ RPC throughput Decline
  27. 27. What is theAxis of Evil?
  28. 28. Game Client Room Lobby Call ‘Enter Room’ RPC Call ‘Authentication’ RPC Thread Blocking Return ‘Authentication’ RPC Return ‘Enter Room’ RPC
  29. 29. Inherent Limitation of RPC
  30. 30. Easy to write code because it’s allcontained inside one function.
  31. 31. Can not split code because it’s allcontained inside one function.
  32. 32. RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …); …. return result;}
  33. 33. What We Want
  34. 34. RPC Function Body Non-Blocking
  35. 35. What is theEssence ofProblems?
  36. 36. Request & Wait
  37. 37. Solution
  38. 38. AsynchronousProgramming
  39. 39. Request(callback, …)
  40. 40. Pros:Non-Blocking
  41. 41. Cons:Can’t Write Sequential CodeCan’t Use Stack VariableCan’t Split Programming Construct…
  42. 42. Too Difficult!!
  43. 43. Coroutine
  44. 44. Similar to Thread
  45. 45. Line of Execution
  46. 46. Stack &Local Variable
  47. 47. But
  48. 48. Non-preemptive
  49. 49. Instruction:YieldYield BreakResume
  50. 50. Who does provide Coroutine?
  51. 51. C# Erlang HaskellJavaScript(since 1.7) Lua Perl Python(since 2.5) Ruby …
  52. 52. What about C++?
  53. 53. On Your Own!
  54. 54. Windows: FiberLinux:getcontext/setcontextmakecontextswapcontext
  55. 55. AsynchronousProgramming + Coroutine
  56. 56. Non-Blocking & WritingSequential Code
  57. 57. Solution forStarvation
  58. 58. BeforeTask A Task B1: Lock 1: Sleep 0msec2: Sleep 2msec3: Unlock
  59. 59. AfterTask A Task BThread A 1: Sleep 0msec1: Create & Resume Coroutine 2: Request Lock 3: Yield Non-BlockingThread A’4: Resume 5: Sleep 2msec 6: Yield Break7: Unlock Sequential Code
  60. 60. Before 1000Process time(ms) 995 Task B 990 Task A 985 1 2 3 4 5 6 7 8 9 # of Thread
  61. 61. After 1500Process time(ms) 1000 Task B 500 Task A 0 1 2 3 4 5 6 7 8 9 # of Thread
  62. 62. Solution forDeadlock Hold & Wait
  63. 63. BeforeThread A Thread B1A: Lock 1B: Lock2A: Request 2B: Send Response3A: Wait for Response 3B: Unlock4A: Do something with Response5A: Unlock
  64. 64. AfterThread A Thread B1A: Lock 1B: Lock2A: Create & Resume Coroutine 2B: Send Response 3A: Request 3B: Unlock 4A: Yield Non-Blocking5A: UnlockThread A’(invoked by response)6A’: Lock7A’: Resume 8A’: Do something with response 9A’: Yield Break10A’: Unlock Sequential Code
  65. 65. Solution forMy Problem
  66. 66. Game Client Room Lobby Call ‘Enter Room’ RPC Call ‘Authentication’ RPC Thread Blocking Return ‘Authentication’ RPC Return ‘Enter Room’ RPC
  67. 67. BeforeRoom Thread Lobby Service1R: Call ‘Authentication’ RPC function 1L: Process the request2R: Wait for the response 2L: Send back the response3R: Process
  68. 68. AfterRoom Thread Lobby Service1R: Create & Resume Coroutine 1L: Process the request 2R: Request 2L: Send back the response 3R: Yield Non-BlockingRoom Thread’(invoked by response)4R’: Resume 5R’: Process 6R’: Yield Break Sequential Code
  69. 69. Request & WaitRequest & Yield
  70. 70. Non-Blocking & WritingSequential Code
  71. 71. Coroutine(Fiber) User-mode Scheduling Kernel-mode SchedulingThread CPU CPU CPU CPU
  72. 72. RPCCoroutine Execution Pool Thread Pool RPC Task Queue Network I/O Thread Pool
  73. 73. Network I/O RPC Task RPC Execution Coroutine Thread Queue Thread PoolNetwork Event task enqueue task dequeue acquire coroutine coroutine RPC start Execution yield returnNetwork resume Event task resume enqueue task dequeue resume yield break release coroutine pooling
  74. 74. Network I/O RPC Task RPC Execution RPC Execution Coroutine Thread Queue Thread 1 Thread 2 PoolNetwork Event task enqueue task dequeue acquire coroutine coroutine RPC resume Execution yield returnNetwork resume Event task resume enqueue task dequeue resume yield break release coroutine pooling
  75. 75. RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …); …. return result;}
  76. 76. RPC::Result* JoinRoom(…){ … RPC::Result answer = RPC::SyncCallYield(“VerifyLobbyUserToken”, …); …. return result;}
  77. 77. T BlockingP YieldS # of Thread
  78. 78. Wrap-up
  79. 79. “The free lunch is OVER.” Write Multithreaded Application CONCURRENCY
  80. 80. Thread Blocking is Evil Starvation Deadlock
  81. 81. AsynchronousProgramming + Coroutine
  82. 82. Non-Blocking + WritingSequential Code
  83. 83. Request & WaitRequest & Yield
  84. 84. Inspired by Jeffrey Richter ‘Simplified APM WithThe AsyncEnumerator’
  85. 85. Pictures fromwww.Istockphoto.comwww.Flickr.com

×