SlideShare une entreprise Scribd logo
1  sur  87
Thread Blocking Is Evil
Game Network Component Team
          Kim, J.H.
CONCURRENCY
Why is
Thread Blocking
  a Concern?
“The Free Lunch
   is OVER.”


                Herb Sutter
             December 2004
Clock speed
Execution optimization
Cache
Hyperthreading
Multicore
Cache
Write Multithreaded
    Application
CONCURRENCY
Waiting in Line
Thread
Blocking
Starvation



             Deadlock
Starvation
Task A           Task B
1: Lock          1: Sleep 0 msec
2: Sleep 1msec
3: Unlock
1000
Process time(ms)




                   995

                                                                    Task B
                   990
                                                                    Task A

                   985
                          1   2   3   4     5       6   7   8   9


                                      # of Thread
Deadlock
 Hold & Wait
Thread A                         Thread B
1A: Lock                         1B: Lock
2A: Request                      2B: Send Response
3A: Wait for Response            3B: Unlock
4A: Do something with Response
5A: Unlock
Thread A                         Thread B
1A: Lock                         1B: Lock
2A: Request                      2B: Send Response
3A: Wait for Response            3B: Unlock
4A: Do something with Response
5A: Unlock
My
Problem
Framework for
Lobby-Room style
     Game
Remote Procedure Call
Easy to write code
  because it’s all
contained inside one
      function.
‘Enter Room’ RPC          ‘Authentication’ RPC
Game Client                      Room                          Lobby
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
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
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
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
What is the
Axis of Evil?
Game Client                           Room                               Lobby

              Call ‘Enter Room’ RPC

                                             Call ‘Authentication’ RPC

               Thread
               Blocking                      Return ‘Authentication’ RPC



              Return ‘Enter Room’ RPC
Inherent Limitation
      of RPC
Easy to write code
  because it’s all
contained inside one
      function.
Can not split code
  because it’s all
contained inside one
      function.
RPC::Result* JoinRoom(…)
{
   …
   RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …);
   ….
   return result;
}
What We Want
RPC Function Body


  Non-Blocking
What is the
Essence of
Problems?
Request & Wait
Solution
Asynchronous
Programming
Request(callback, …)
Pros:
Non-Blocking
Cons:
Can’t Write Sequential Code
Can’t Use Stack Variable
Can’t Split Programming Construct
…
Too Difficult!!
Coroutine
Similar to Thread
Line of Execution
Stack
      &
Local Variable
But
Non-preemptive
Instruction:
Yield
Yield Break
Resume
Who does provide
  Coroutine?
C#
      Erlang
      Haskell
JavaScript(since 1.7)
        Lua
        Perl
 Python(since 2.5)
       Ruby
         …
What about C++?
On Your Own!
Windows: Fiber

Linux:
getcontext/setcontext
makecontext
swapcontext
Asynchronous
Programming
      +
  Coroutine
Non-Blocking
      &
    Writing
Sequential Code
Solution
   for
Starvation
Before

Task A           Task B
1: Lock          1: Sleep 0msec
2: Sleep 2msec
3: Unlock
After

Task A                                 Task B
Thread A                               1: Sleep 0msec
1: Create & Resume   Coroutine
                     2: Request Lock
                     3: Yield          Non-Blocking
Thread A’
4: Resume
                     5: Sleep 2msec
                     6: Yield Break
7: Unlock
                                Sequential
                                  Code
Before




                            1000
Process time(ms)




                            995

                                                                             Task B
                            990
                                                                             Task A

                            985
                                   1   2   3   4     5       6   7   8   9


                                               # of Thread
After




                           1500
Process time(ms)




                           1000

                                                                            Task B
                           500
                                                                            Task A

                             0
                                  1   2   3   4     5       6   7   8   9


                                              # of Thread
Solution
    for
Deadlock
 Hold & Wait
Before

Thread A                         Thread B
1A: Lock                         1B: Lock
2A: Request                      2B: Send Response
3A: Wait for Response            3B: Unlock
4A: Do something with Response
5A: Unlock
After

Thread A                                    Thread B
1A: Lock                                    1B: Lock
2A: Create & Resume     Coroutine           2B: Send Response
                        3A: Request         3B: Unlock
                        4A: Yield           Non-Blocking
5A: Unlock
Thread A’
(invoked by response)

6A’: Lock
7A’: Resume
                        8A’: Do something
                        with response
                        9A’: Yield Break
10A’: Unlock
                                Sequential
                                  Code
Solution
    for
My Problem
Game Client                           Room                               Lobby

              Call ‘Enter Room’ RPC

                                             Call ‘Authentication’ RPC

               Thread
               Blocking                      Return ‘Authentication’ RPC



              Return ‘Enter Room’ RPC
Before

Room Thread                              Lobby Service
1R: Call ‘Authentication’ RPC function   1L: Process the request
2R: Wait for the response                2L: Send back the response
3R: Process
After

Room Thread                                Lobby Service
1R: Create & Resume     Coroutine          1L: Process the request
                        2R: Request        2L: Send back the response
                        3R: Yield          Non-Blocking
Room Thread’
(invoked by response)

4R’: Resume
                        5R’: Process
                        6R’: Yield Break


                                Sequential
                                  Code
Request & Wait

Request & Yield
Non-Blocking
      &
    Writing
Sequential Code
Coroutine
(Fiber)
                                      User-mode Scheduling
                                    Kernel-mode Scheduling
Thread




            CPU   CPU   CPU   CPU
RPC
Coroutine
                              Execution
  Pool
                             Thread Pool



            RPC Task Queue



             Network I/O
             Thread Pool
Network I/O             RPC Task             RPC Execution                                      Coroutine
      Thread                Queue                  Thread                                            Pool
Network
 Event             task
                 enqueue                task
                                      dequeue
                                                                   acquire coroutine
                                                                                       coroutine
                                                                     RPC
                                                          start    Execution



                                                          yield return
Network          resume
 Event             task               resume
                 enqueue
                                        task
                                      dequeue
                                                          resume



                                                           yield break

                                                                   release coroutine                           pooling
Network I/O             RPC Task             RPC Execution                  RPC Execution                  Coroutine
      Thread                Queue                 Thread 1                       Thread 2                       Pool
Network
 Event             task
                 enqueue                task
                                      dequeue
                                                                   acquire coroutine
                                                                                               coroutine
                                                                     RPC
                                                         resume    Execution



                                                          yield return
Network          resume
 Event             task               resume
                 enqueue
                                        task
                                      dequeue
                                                                           resume



                                                                         yield break

                                                                                          release coroutine               pooling
RPC::Result* JoinRoom(…)
{
   …
   RPC::Result answer = RPC::SyncCall(“VerifyLobbyUserToken”, …);
   ….
   return result;
}
RPC::Result* JoinRoom(…)
{
   …
   RPC::Result answer = RPC::SyncCallYield(“VerifyLobbyUserToken”, …);
   ….
   return result;
}
T                 Blocking

P                 Yield


S


    # of Thread
Wrap-up
“The free lunch
  is OVER.”
   Write Multithreaded
        Application
       CONCURRENCY
Thread Blocking is Evil

  Starvation   Deadlock
Asynchronous
Programming
      +
  Coroutine
Non-Blocking
      +
    Writing
Sequential Code
Request & Wait

Request & Yield
Inspired by
  Jeffrey Richter
 ‘Simplified APM With
The AsyncEnumerator’
Pictures from
www.Istockphoto.com
www.Flickr.com

Contenu connexe

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Thread Blocking is Evil