Multi-Threading In C
A quick overview of multithreading in c language and the different functions and functionalities that are available.## Requirements
- Basics understandings in programing.
- Basics in c / cpp programming.
What is MultiThreading
Usually when we write programs we have only one line of execution, what i mean by line of exeuction is that there is only one instruction of our code being executed by the CPU at a certain time.
[Image showing the line of execution]
Back in the days this was the standar for all programs since all computers / machines had only one CPU and thus can't execute more then one instruction at time, But now days things have changed and computers have evolved providing us with what we call multi corse cpu, each core is a separate cpu that can handle instruction on it's own.
[OLD_CPU] [NEW_CPU]
And us programmers can benefit from this cpu cores and make our programs execute a lot faster by having multiple instructions / tasks being executed simultaneously.
Why we need MultiThreading
Let's imagine we have a function called **A**, in order for A to be executed it need to read two files. What we can tend to do is to read the first file then read the second file and finally execute the function A providing it with the necessary files. Doing it this way will not profit from the multiple CPU corses, since the two files are completely separate what we could do is read both files at the same time and the provide them to the function A.
[Image showing the main program calling the two threads then giving result to A]
[Code editor showing code]
Doing this will take profit from the available cpu cores and make our program twice faster.
MultiThreading Examples
- UI rendering
- Summing a matrix
- File reading
- ...
MultiTheading Drawbacks
- enabling multithreading when we have only 1 core
- creating more threads the we cores
MultiThreading C Functions
- A
- B
- C
- D
- E
## MultiThreading Example
https://www.ibm.com/docs/en/zos/2.3.0?topic=files-pthreadh-thread-interfaces
- present a problematic of why we need threads, use illustrations that shows how code is executed only on 1 CPU core of our computer. do a before having multiple cpu_cores and after
- introduce threads and multiple usecases (operations, server, ui, ...) and explain them
- present the pthreads.h library, it's functions, types, variables and definitions with uses cases and illustrations for each function
- solve the problem you presented while presenting "why wee neded threads"
- don't forget foot notes