Practical multithreading tutorials. Languages supported: C++, Java, C#, Python, Javascript/Nodejs.
This repo helps you to practise multithreading in a logical sequence, which is divided into several demonstrations. Plus, you could apply your learning better by doing exercises.
The repo consists of two main sections:
All the demos (and exers) are really simple, easy to understand, even for difficult terms.
If you find it helpful, please give my repo a star. Thank you.
Author: Thanh Nguyen
This repo is licensed under the 3-Clause BSD License.
Directory name | Description |
---|---|
cpp-std |
C++20 std threading |
cpp-pthread |
C++11 POSIX threading |
cpp-boost |
C++98 Boost threading |
csharp |
C# 7.3 with Dot Net 6 |
java |
Java JDK 17 |
python |
Python 3.10 |
js-nodejs |
Javascript ES2019/Nodejs 18 |
Special notes for C++ demos/exers: Please read the specified readme.md
in corresponding directory.
The notes and articles are the additional resources for the source code, which guides you for better research, step by step. You may consider it the comment/description at the beginning of the source code.
ORIGINAL SOURCE CODE FILE SOURCE CODE FILE NOTES AND ARTICLES
------------------------------ ------------------------------ ----------------------
| | | | | |
| /* THE COMMENTS... */ | | | | THE COMMENTS |
| | | | | |
| #include <iostream> | | #include <iostream> | | |
| using namespace std; | | using namespace std; | | |
| | ===> | | + | |
| int main() { | | int main() { | | |
| cout << "Hello thread"; | | cout << "Hello thread"; | | |
| return 0; | | return 0; | | |
| } | | } | | |
| | | | | |
------------------------------ ------------------------------ ----------------------
There are 2 notes:
For your best result, I strongly recommend that you read notes-demos-exercises.md while enjoying source code (demos and exercises).
This is the roadmap for you, which is composed and researched carefully with all my heart. You should learn in the sequence listed below.
If you just want to learn the basis to understand the taste of multithreading:
If you are oriented to be a Software Developer:
If you really want to do an in-depth research: Learn all!!!
Bob sends four messages to Alice: I love
, you
, not
, her
.
Surprisingly, Alice receives I love
, her
, not
, you
(That means "I love her not you"). So sad!
TRADITIONAL (ONE THREAD)
===========================================> Time
Main thread
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
"I love" "you" "not" "her"
MULTITHREADING (FOUR THREADS)
===========================================> Time
~~~~~~~~~~~~>
"I love"
~~~~~~~~~~~~>
"you"
~~~~~~~~~~~~>
"not"
~~~~~~~~~~~~>
"her"
If you use multithreading or something similar, the context above is truly possible. The reason is that multithreading allows four messages to be sent in parallel, so message order is changed unpredictably when they come to Alice.
In a traditional simple app, there is only one thread (the "main thread"). If you apply multithreading then your app may have multiple threads (including the "main thread").
By learning multithreading:
So, why multithreading?
Multithreaded programs can improve performance compared to traditional simple programs (which use only a single thread).
Multithreading is used as an underlying technique in various fields:
Benefits of multithreading:
Improving application responsiveness.
Using multiprocessors efficiently.
Improving throughput.
Program structure simplification.
Using fewer system resources.
Better communication.
If you want to explore more articles, read here: notes-articles.md.
Article references:
All general references in my repo.
Read here: references.md.