In this recipe, we will be creating a thread to perform a task. In this task, we will display the sequence numbers from 1 to 5. The focus of this recipe is to learn how a thread is created and how the main thread is asked to wait until the thread finishes its task.
Performing a task with a single thread
How to do it…
- Define a variable of the type pthread_t to store the thread identifier:
pthread_t tid;
- Create a thread and pass the identifier that was created in the preceding step to the pthread_create function. The thread is created with the default attributes. Also, specify a function that needs to be executed to create the thread:
pthread_create(&tid, NULL, runThread, NULL);
- In the function, you will be displaying...