I show you a small tutorial about the multithreading.
Why use of multithreading?
If you want will develop, a software which manage many data, for some images, databases or anything. If you make a classical loops, you software don't work in same time, because a loop lock all future code when it works.
But if you create a thread, it runs in parallel with your code (like a executable) and it didn't lock anything except it.
Well for use it, you need some precautions:
Don't forget that, this is your os will be run the thread when it want, and usually it don't use the creating order.
We need to manage the synchronization between threads.
For do it, we have two tools to use:
- Semaphores
- Mutex
We talk about (mutex) mutual exclusion in another page and after more time.
Two method exist with semaphore :
- wait
- signal
The signal method, increment the semaphore et wake up a thread.
Even if semaphore use look like easy, nicely use semaphores is not a piece of cake.
An american teacher of university "Allen B. Downey" , wrote a book about semaphore usage "The Little Book of Semaphores". In his book he gives some patterns for use semaphores in some situations.
The Little Book of Semaphores
I designed some examples from this book and about variousing scenarios
Examples :
- The rendezvous
- The mutex
- Runners (part 1)
- Runners (part 2)
- Runners (part 3)
- To following soon (Ther producer and the counsumer)