- Define two variables of the pthread_t type to store two thread identifiers. Also, define a mutex object:
pthread_t tid1,tid2;pthread_mutex_t lock;
- Invoke the pthread_mutex_init method to initialize the mutex object with the default mutex attributes:
pthread_mutex_init(&lock, NULL)
- Invoke the pthread_create function twice to create two threads, and assign the identifiers that we created in step 1. Execute a function for creating the two threads:
pthread_create(&tid1, NULL, &runThread, NULL);pthread_create(&tid2, NULL, &runThread, NULL);
- In the function, the pthread_mutex_lock method is invoked and the mutex object is passed to it to lock it:
pthread_mutex_lock(&lock);
- Invoke the pthread_self method and assign the ID ...