In this recipe, you will learn how to multitask by executing two threads in parallel. Both of the threads will do their tasks independently. As the two threads will not be sharing a resource, there will not be a situation of race condition or ambiguity. The CPU will execute any thread randomly at a time, but finally, both of the threads will finish the assigned task. The task that the two threads will perform is displaying the sequence of numbers from 1 to 5.
Performing multiple tasks with multiple threads
How to do it…
- Define two variables of the type pthread_t to store two thread identifiers:
pthread_t tid1, tid2;
- Invoke the pthread_create function twice to create two threads, and assign the identifiers that we...