Debugging threads with HP Wilde Beest
TIP:
• If the remaining segments of the application require access to the locked mutex,
modify the code segment of the terminating thread to unlock the mutex before it
terminates.
• If the termination is the result of an exception, then consider using a condition
handler (in C++) or POSIX Threads library TRY/FINALLY blocks.
Problem: The thread waits on a condition variable for which the associated mutex is not locked.
Consider the following scenario:
A function has a thread which is associated to the mutex MX. The function calls the
POSIX Thread Library routine pthread_cond_wait() beforeMX is locked.
Consider the following example enh_thr_cv_wait_no_mx.c:
#include pthread.h
#include stdlib.h
#include errno.h
pthread_mutex_t job_lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t job_lock2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t job_cv = PTHREAD_COND_INITIALIZER;
extern void fatal_error(int err, char *f);
void
producer_thread(pthread_mutex_t* job_lock)
{
int ret_val;
/* Acquire the associated mutex lock */
if ((ret_val = pthread_mutex_lock(job_lock)) != 0)
fatal_error(ret_val, "p mtx_lock failed");
/* Signal the condvar to wakeup one thread */
if ((ret_val = pthread_cond_signal(&job_cv)) != 0)
fatal_error(ret_val, "cond_signal failed");
/* Release the associated mutex */
if ((ret_val = pthread_mutex_unlock(job_lock)) != 0)
fatal_error(ret_val, "mtx_unlock failed");
}
void
consumer_thread(pthread_mutex_t* job_lock)
{
int ret_val;
pthread_cond_wait(&job_cv, job_lock);
}
Modes of Thread debugging in HP WDB 31