Metadata
-
Date
-
Tagged
-
Older post
-
Newer post
Concurrent vs parallel
The concepts of concurrency and parallelism are used to fully use the computational power of a CPU.
The general idea is to do as much work as possible in as small a time as possible.
-
A CPU core is a place that can perform work, it can execute lines of code. It can only do one thing at a time. Modern computers nearly all have multiple cores.
-
A thread is a collection of work with lines of code that should be executed in order, top to bottom.
Concurrent execution of a large piece of work means that piece was broken into smaller pieces that were distributed over multiple threads.
The computer works on completing those individual threads by executing code on each thread little by little, often switching between threads before that thread’s work is finished.
Parallel execution of a large piece of code means multiple threads are being executed at the same time.
Since a core can only do one thing at a time, this means multiple cores are needed for parallelism.
Nearly all code that is executed in parallel is also concurrent.
To always achieve the same end result, code in threads is written so it is able to execute independent from what other threads are doing.
This allows multiple threads to execute at the exact same time while still reaching the same result every time the entire program is ran.
Demos
Hover over the demo to start it.
The line represents a core executing lines of code.
A core can work on one thread at a time.
Sequential
One thread, one CPU core
Not a lot to say here, the lines of code in a thread get executed one after the other, in order.
Core
Thread
Concurrency
Two threads, one CPU core
The OS can decide which thread a CPU core works on, so the order in which threads are executed is not guaranteed.
The order of tasks within a single thread is.
Core
Thread
Thread
Parallelism
Two threads, two CPU cores
Using multiple cores means multiple things can be executed at once.
Core
Thread
Core
Thread
Make them kiss
Four threads, two CPU cores
Using multiple cores means multiple things can be executed at once.
A single core can still only do one thing at a time.
Core
Thread
Thread
Core
Thread
Thread