Debugging threads with HP Wilde Beest
#define check_error(return_val, msg) { \
if (return_val != 0) \
fatal_error(return_val, msg); \
}
main()
{
pthread_t tid1, tid2;
int ret_val;
alarm (20);
/* Create two threads to do the work */
ret_val = pthread_create(&tid1, (pthread_attr_t *)NULL,
(void *(*)())consumer_thread, (void *) &job_lock1);
check_error(ret_val, "pthread_create 1 failed");
ret_val = pthread_create(&tid2, (pthread_attr_t *)NULL,
(void *(*)())producer_thread, (void *) &job_lock1);
check_error(ret_val, "pthread_create 2 failed");
/* Wait for the threads to finish */
ret_val = pthread_join(tid1, (void **)NULL);
check_error(ret_val, "pthread_join: tid1");
ret_val = pthread_join(tid2, (void **)NULL);
check_error(ret_val, "pthread_join: tid2");
exit(0);
}
void
fatal_error(int err_num, char *function)
{
char *err_string;
err_string = strerror(err_num);
fprintf(stderr, "%s error: %s\n", function, err_string);
exit(-1);
}
This scenario, where a thread waits on a conditional variable before the associated
mutex is locked, is a potential cause of unpredictable results in POSIXlibrary.
The following command in HP WDB enables you to check this condition in a threaded
application:
set thread-check cv-wait-no-mx[on|off]
In such a scenario, the debugger transfers the execution control to the user and displays
a warning message.
The following is a segment of the HP WDB output:
32