SlideShare a Scribd company logo
1 of 16
Download to read offline
Threads are evil
Parallelism meets programming reality
Next slides contain content which may be shocking
Northeast blackout
widespread power outage
throughout U.S. and Canada
affecting 55 million people
August, 14th 2003
World’s second biggest
power outage
Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
Source:Wikipedia
A race condition occurred in General Electric Energy's Unix-based energy
management system. Once triggered, the bug stalled FirstEnergy's control room
alarm system for over an hour. System operators were unaware of the malfunction.
The failure deprived them of both audio and visual alerts for important changes in
system state.
Unprocessed events queued up the primary and backup server. The server failures
slowed the screen refresh rate of the operators' consoles from 1–3 seconds to 59
seconds per screen. The lack of alarms prevented operators control a simple failure
which led to the world’s second biggest power outage.
What ?
What is a thread?
A thread is a programming primitive. It is the
smallest sequence of programmed
instructions that can be managed
independently by the scheduler of the OS.
All memory within the process is shared
across the threads.
Sections that need to be executed
sequentially need to be protected with a
mutex, ...
The problem:
Uncareful programming leads to unpredictable behavior
and hard to reproduce bugs.
Non-atomic data needs to be protected
with mutexes, semaphores, RW-locks, ...
● Hard for programmers to reach watertight functionality
● Bugs do not show up that easy
○ Eg Heisenbug
● Little support from tools to reveal concurrency issues
● Mutexes, semaphores, … are expensive to use.
Alternatives
Futures and promises
Offloads method into separate thread. Sends a
signal when finished.
Implemented by a thread. Securing shared data
access is still required.
Uses threadpool: cheaper to construct
Multi-process
Split algorithm into separate process which can
be monitored.
To interact with process:
● IPC required
● Cmd line or parsable output is required.
What have we learned?
Mutexes Atomic types Futures and
promises
Single threaded
apps
OS primitive Supported by many CPU
architectures
Alternative for apps with
event loop.
Easy to program, easy
to maintain.
Thread is sleeping while
waiting
Cheaper to use than
mutex
Be aware of concurrent
data access.
Requires IPC to
feedback results or
interact
Related to semaphores
which have less
overhead
Be aware of memory
re-ordering
Supported by std, Qt,
boost
Preferred by Google’s
and Apple’s coding
guidelines
What have we learned?
Mutexes Atomic types Futures and
promises
Single threaded
apps
OS primitive Supported by many CPU
architectures
Alternative for apps with
event loop.
Easy to program, easy
to maintain.
Thread is sleeping while
waiting
Cheaper to use than
mutex
Be aware of concurrent
data access.
Requires IPC to
feedback results or
interact
Related to semaphores
which have less
overhead
Be aware of memory
re-ordering
Supported by std, Qt,
boost
Preferred by Google’s
and Apple’s coding
guidelines
What have we learned?
Mutexes Atomic types Futures and
promises
Single threaded
apps
OS primitive Supported by many CPU
architectures
Alternative for apps with
event loop.
Easy to program, easy
to maintain.
Thread is sleeping while
waiting
Cheaper to use than
mutex
Be aware of concurrent
data access.
Requires IPC to
feedback results or
interact
Related to semaphores
which have less
overhead
Be aware of memory
re-ordering
Supported by std, Qt,
boost
Preferred by Google’s
and Apple’s coding
guidelines
What have we learned?
Mutexes Atomic types Futures and
promises
Single threaded
apps
OS primitive Supported by many CPU
architectures
Alternative for apps with
event loop.
Easy to program, easy
to maintain.
Thread is sleeping while
waiting
Cheaper to use than
mutex
Be aware of concurrent
data access.
Requires IPC to
feedback results or
interact
Related to semaphores
which have less
overhead
Be aware of memory
re-ordering
Supported by std, Qt,
boost
Preferred by Google’s
and Apple’s coding
guidelines
What have we learned?
Mutexes Atomic types Futures and
promises
Single threaded
apps
OS primitive Supported by many CPU
architectures
Alternative for apps with
event loop.
Easy to program, easy
to maintain.
Thread is sleeping while
waiting
Cheaper to use than
mutex
Be aware of concurrent
data access.
Requires IPC to
feedback results or
interact
Related to semaphores
which have less
overhead
Be aware of memory
re-ordering
Supported by std, Qt,
boost
Preferred by Google’s
and Apple’s coding
guidelines
Benchmark
Demo app calculating prime numbers
and adding results to a list during 30
seconds
Code on https://github.com/frederikvannoote/becpp
List
Prime
number
calculator
Prime
number
calculator
Prime
number
calculator
Benchmark
Demo app calculating prime numbers
and adding results to a list during 30
seconds
The conclusion
Mutexes are expensive
and error prone.
Atomics are cheaper
Multi-process avoids
sharing data all together
and gives best
performance, but require
IPC.
More
Advanced threading libraries:
● Threadweaver
● Grand Central Dispatch
Apple coding guidelines:
● https://developer.apple.com/library/content/documentation/Cocoa/Conc
eptual/Multithreading/AboutThreads/AboutThreads.html

More Related Content

Similar to Threads are evil

Embedded system
Embedded systemEmbedded system
Embedded system
12lakshmi
 
Choosing the right parallel compute architecture
Choosing the right parallel compute architecture Choosing the right parallel compute architecture
Choosing the right parallel compute architecture
corehard_by
 

Similar to Threads are evil (20)

Operating system lecture1
Operating system lecture1Operating system lecture1
Operating system lecture1
 
Evolution of the Windows Kernel Architecture, by Dave Probert
Evolution of the Windows Kernel Architecture, by Dave ProbertEvolution of the Windows Kernel Architecture, by Dave Probert
Evolution of the Windows Kernel Architecture, by Dave Probert
 
Oct2009
Oct2009Oct2009
Oct2009
 
Oslecture1
Oslecture1Oslecture1
Oslecture1
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Embedded Intro India05
Embedded Intro India05Embedded Intro India05
Embedded Intro India05
 
Architecture of web servers
Architecture of web serversArchitecture of web servers
Architecture of web servers
 
Unit 1_Operating system
Unit 1_Operating system Unit 1_Operating system
Unit 1_Operating system
 
Understanding the characteristics of android wear os
Understanding the characteristics of android wear osUnderstanding the characteristics of android wear os
Understanding the characteristics of android wear os
 
Lecture 5: Software platforms and services
Lecture 5: Software platforms and services Lecture 5: Software platforms and services
Lecture 5: Software platforms and services
 
Embeddedsystems
EmbeddedsystemsEmbeddedsystems
Embeddedsystems
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
es1-150721100817-lva1-app6891.pdf
es1-150721100817-lva1-app6891.pdfes1-150721100817-lva1-app6891.pdf
es1-150721100817-lva1-app6891.pdf
 
Underlying principles of parallel and distributed computing
Underlying principles of parallel and distributed computingUnderlying principles of parallel and distributed computing
Underlying principles of parallel and distributed computing
 
Embedded system
Embedded systemEmbedded system
Embedded system
 
Principles of operating system
Principles of operating systemPrinciples of operating system
Principles of operating system
 
Choosing the right parallel compute architecture
Choosing the right parallel compute architecture Choosing the right parallel compute architecture
Choosing the right parallel compute architecture
 
Distributed Computing
Distributed ComputingDistributed Computing
Distributed Computing
 
Software and the Concurrency Revolution : Notes
Software and the Concurrency Revolution : NotesSoftware and the Concurrency Revolution : Notes
Software and the Concurrency Revolution : Notes
 
Chapter_01.pptx
Chapter_01.pptxChapter_01.pptx
Chapter_01.pptx
 

Recently uploaded

21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
vimal412355
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Overview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsOverview of Transformation in Computer Graphics
Overview of Transformation in Computer Graphics
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
Call for Papers - Journal of Electrical Systems (JES), E-ISSN: 1112-5209, ind...
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 

Threads are evil

  • 1. Threads are evil Parallelism meets programming reality
  • 2. Next slides contain content which may be shocking
  • 3. Northeast blackout widespread power outage throughout U.S. and Canada affecting 55 million people August, 14th 2003 World’s second biggest power outage Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec Source:Wikipedia A race condition occurred in General Electric Energy's Unix-based energy management system. Once triggered, the bug stalled FirstEnergy's control room alarm system for over an hour. System operators were unaware of the malfunction. The failure deprived them of both audio and visual alerts for important changes in system state. Unprocessed events queued up the primary and backup server. The server failures slowed the screen refresh rate of the operators' consoles from 1–3 seconds to 59 seconds per screen. The lack of alarms prevented operators control a simple failure which led to the world’s second biggest power outage.
  • 4. What ? What is a thread? A thread is a programming primitive. It is the smallest sequence of programmed instructions that can be managed independently by the scheduler of the OS. All memory within the process is shared across the threads. Sections that need to be executed sequentially need to be protected with a mutex, ...
  • 5. The problem: Uncareful programming leads to unpredictable behavior and hard to reproduce bugs.
  • 6. Non-atomic data needs to be protected with mutexes, semaphores, RW-locks, ... ● Hard for programmers to reach watertight functionality ● Bugs do not show up that easy ○ Eg Heisenbug ● Little support from tools to reveal concurrency issues ● Mutexes, semaphores, … are expensive to use.
  • 7. Alternatives Futures and promises Offloads method into separate thread. Sends a signal when finished. Implemented by a thread. Securing shared data access is still required. Uses threadpool: cheaper to construct Multi-process Split algorithm into separate process which can be monitored. To interact with process: ● IPC required ● Cmd line or parsable output is required.
  • 8. What have we learned? Mutexes Atomic types Futures and promises Single threaded apps OS primitive Supported by many CPU architectures Alternative for apps with event loop. Easy to program, easy to maintain. Thread is sleeping while waiting Cheaper to use than mutex Be aware of concurrent data access. Requires IPC to feedback results or interact Related to semaphores which have less overhead Be aware of memory re-ordering Supported by std, Qt, boost Preferred by Google’s and Apple’s coding guidelines
  • 9. What have we learned? Mutexes Atomic types Futures and promises Single threaded apps OS primitive Supported by many CPU architectures Alternative for apps with event loop. Easy to program, easy to maintain. Thread is sleeping while waiting Cheaper to use than mutex Be aware of concurrent data access. Requires IPC to feedback results or interact Related to semaphores which have less overhead Be aware of memory re-ordering Supported by std, Qt, boost Preferred by Google’s and Apple’s coding guidelines
  • 10. What have we learned? Mutexes Atomic types Futures and promises Single threaded apps OS primitive Supported by many CPU architectures Alternative for apps with event loop. Easy to program, easy to maintain. Thread is sleeping while waiting Cheaper to use than mutex Be aware of concurrent data access. Requires IPC to feedback results or interact Related to semaphores which have less overhead Be aware of memory re-ordering Supported by std, Qt, boost Preferred by Google’s and Apple’s coding guidelines
  • 11. What have we learned? Mutexes Atomic types Futures and promises Single threaded apps OS primitive Supported by many CPU architectures Alternative for apps with event loop. Easy to program, easy to maintain. Thread is sleeping while waiting Cheaper to use than mutex Be aware of concurrent data access. Requires IPC to feedback results or interact Related to semaphores which have less overhead Be aware of memory re-ordering Supported by std, Qt, boost Preferred by Google’s and Apple’s coding guidelines
  • 12. What have we learned? Mutexes Atomic types Futures and promises Single threaded apps OS primitive Supported by many CPU architectures Alternative for apps with event loop. Easy to program, easy to maintain. Thread is sleeping while waiting Cheaper to use than mutex Be aware of concurrent data access. Requires IPC to feedback results or interact Related to semaphores which have less overhead Be aware of memory re-ordering Supported by std, Qt, boost Preferred by Google’s and Apple’s coding guidelines
  • 13. Benchmark Demo app calculating prime numbers and adding results to a list during 30 seconds Code on https://github.com/frederikvannoote/becpp List Prime number calculator Prime number calculator Prime number calculator
  • 14. Benchmark Demo app calculating prime numbers and adding results to a list during 30 seconds
  • 15. The conclusion Mutexes are expensive and error prone. Atomics are cheaper Multi-process avoids sharing data all together and gives best performance, but require IPC.
  • 16. More Advanced threading libraries: ● Threadweaver ● Grand Central Dispatch Apple coding guidelines: ● https://developer.apple.com/library/content/documentation/Cocoa/Conc eptual/Multithreading/AboutThreads/AboutThreads.html