Mastering pthread_cond_wait: A Step‑by‑Step Guide to Linux Thread Synchronization
This article explains how to use Linux pthread condition variables, covering initialization, the pthread_cond_wait function, signal handling, and a complete C example that demonstrates thread coordination and mutex interaction for reliable multithreaded programming.
The article introduces the Linux POSIX thread condition variable pthread_cond_t and its associated functions, starting with pthread_cond_init (prototype:
extern int pthread_cond_init(pthread_cond_t *__cond, const pthread_condattr_t *__cond_attr);) which initializes a condition variable, and pthread_cond_destroy for cleanup. It also mentions the static initializer PTHREAD_COND_INITIALIZER.
The core function pthread_cond_wait (prototype:
extern int pthread_cond_wait(pthread_cond_t *__cond, pthread_mutex_t *__mutex);) atomically releases the mutex and blocks the calling thread on the condition variable. When the thread is awakened by pthread_cond_signal or pthread_cond_broadcast, it reacquires the mutex and should re‑evaluate the waiting condition, typically inside a while loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* initialize mutex */
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; /* initialize condition variable */
void *thread1(void *);
void *thread2(void *);
int i = 1;
int main(void) {
pthread_t t_a, t_b;
pthread_create(&t_a, NULL, thread1, NULL); /* create thread A */
pthread_create(&t_b, NULL, thread2, NULL); /* create thread B */
pthread_join(t_b, NULL); /* wait for thread B */
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}
void *thread1(void *junk) {
for (i = 1; i <= 9; i++) {
pthread_mutex_lock(&mutex);
if (i % 3 == 0)
pthread_cond_signal(&cond); /* notify thread2 */
else
printf("thread1:%d
", i);
pthread_mutex_unlock(&mutex);
printf("Up Unlock Mutex
");
sleep(1);
}
return NULL;
}
void *thread2(void *junk) {
while (i < 9) {
pthread_mutex_lock(&mutex);
if (i % 3 != 0)
pthread_cond_wait(&cond, &mutex); /* wait */
printf("thread2:%d
", i);
pthread_mutex_unlock(&mutex);
printf("Down Unlock Mutex
");
sleep(1);
}
return NULL;
}The program produces output such as:
thread1:1
Up Unlock Mutex
thread1:2
Up Unlock Mutex
thread2:3
Down Unlock Mutex
... (continues up to 9)Explanation of the execution flow:
When i is not a multiple of three, thread2 calls pthread_cond_wait, releasing the mutex and blocking. thread1 acquires the mutex, prints its value, and when i is a multiple of three, it signals the condition variable, waking thread2.
After being awakened, thread2 reacquires the mutex, prints its value, releases the mutex, and continues the loop.
The example demonstrates that pthread_cond_wait not only works with the condition variable but also atomically releases and reacquires the associated mutex, ensuring correct synchronization between threads.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
