Giter Site home page Giter Site logo

thanhit95 / multi-threading Goto Github PK

View Code? Open in Web Editor NEW
111.0 3.0 35.0 860 KB

Practical multithreading tutorials. Languages supported: C++, Java, C#, Python, Javascript/Nodejs.

License: BSD 3-Clause "New" or "Revised" License

C++ 49.63% Java 18.52% C 0.17% C# 15.88% Python 9.26% JavaScript 6.54%
multithreading thread threading mutex semaphore producer-consumer thread-pool cpp java csharp

multi-threading's Introduction

MULTIPLE THREADING IN PRACTICE

DESCRIPTION

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:

  • "demo" (demostrations).
  • "exer" (exercises).

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 & LICENSE

Author: Thanh Nguyen

This repo is licensed under the 3-Clause BSD License.

 

LANGUAGES SUPPORTED

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

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).

 

ROADMAP FOR THE LEARNERS

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:

  • Demo: hello, join, pass arg, sleep, list-threads, race-condition, mutex, synchronized-block.
  • Exer: max-div.

If you are oriented to be a Software Developer:

  • Demo: hello, join, pass arg, sleep, list-threads, terminate, return-value, exec-service, race-condition, mutex, synchronized-block, deadlock, blocking-queue, atomic.
  • Exer: max-div, producer-consumer, product-matrix, data-server.

If you really want to do an in-depth research: Learn all!!!

 


INTRODUCTION TO MULTITHREADING

GETTING STARTED

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:

  • You get closer to the operating system.
  • You can understand various terms: concurrency, parallel, asynchronous, synchronization.
  • You have additional knowledge to learn asynchronous programming and parallel programming.

So, why multithreading?

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:

  • Web browsers (Chrome, Edge, Firefox...).
  • Web servers.
  • Graphic editors (Adobe Photoshop, Corel Draw...).
  • Computer games.
  • Database management systems.
  • Networking programming.
  • Video encoders.
  • And more...

Benefits of multithreading:

  • Improving application responsiveness.

    • Any program in which many activities are not dependent upon each other can be redesigned so that each activity is defined as a thread. For example, the user of a multithreaded GUI does not have to wait for one activity to complete before starting another.
  • Using multiprocessors efficiently.

    • Typically, applications that express concurrency requirements with threads need not take into account the number of available processors. The performance of the application improves transparently with additional processors.
    • Numerical algorithms and applications with a high degree of parallelism, such as matrix multiplications, can run much faster when implemented with threads on a multiprocessor.
  • Improving throughput.

    • Many concurrent compute operations and I/O requests within a single process.
  • Program structure simplification.

    • Threads can be used to simplify the structure of complex applications, such as server-class and multimedia applications. Simple routines can be written for each activity, making complex programs easier to design and code, and more adaptive to a wide variation in user demands.
  • Using fewer system resources.

    • Threads impose minimal impact on system resources. Threads require less overhead to create, maintain, and manage than a traditional process.
  • Better communication.

    • Thread synchronization functions can be used to provide enhanced process-to-process communication.
    • In addition, sharing large amounts of data through separate threads of execution within the same address space provides extremely high-bandwidth, low-latency communication between separate tasks within an application.

 

If you want to explore more articles, read here: notes-articles.md.

 

Article references:

 


REFERENCES

All general references in my repo.

Read here: references.md.

multi-threading's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

multi-threading's Issues

Improving scope handling of std::unique_lock to avoid premature unlock

Line 40: uniquelk(mut); https://github.com/thanhit95/multi-threading/blob/main/cpp/cpp-std/demo17c-reentrant-lock.cpp

I've encountered a potential issue in the synchronization logic involving std::recursive_mutex and std::unique_lock. The current implementation creates temporary std::unique_lock objects that are not assigned to variables. This results in the locks being released immediately after their creation due to the temporary objects going out of scope. I wonder if this approach was chosen for specific reasons that I might not be aware of.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.