pthread_t tid1;
pthread_t tid2;
+ /* Initialize synchronization objects. */
sem_init(&s_sem, 0, 0);
pthread_cond_init(&s_cond, 0);
pthread_mutex_init(&s_mutex1, 0);
pthread_mutex_init(&s_mutex2, 0);
+
+ /* Create two threads. */
pthread_create(&tid1, 0, &thread1, 0);
pthread_create(&tid2, 0, &thread2, 0);
+
+ /* Wait until both threads have called sem_post(). */
sem_wait(&s_sem);
sem_wait(&s_sem);
+
+ /* Wait until both threads are waiting inside pthread_cond_wait(). */
pthread_mutex_lock(&s_mutex1);
pthread_mutex_lock(&s_mutex2);
pthread_mutex_unlock(&s_mutex2);
pthread_mutex_unlock(&s_mutex1);
+
+ /* Signal s_cond twice. */
pthread_cond_signal(&s_cond);
pthread_cond_signal(&s_cond);
+
+ /* Join both threads. */
pthread_join(tid1, 0);
pthread_join(tid2, 0);
+
return 0;
}