|
From: Martin H. <mar...@ci...> - 2011-08-19 15:42:29
|
Hi all,
I have had some luck finding the error which caused this Helgrind
assertion to fail. Not surprisingly it was an error in the debugged
program's code.
A pthread condition variable was being destroyed just after being
signalled, before the other thread would return from the wait function.
Short code reproducing the error is given below.
// CODE BEGIN
#include <iostream>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t thread;
bool ready = false;
void *ThreadFunction(void *ptr) {
pthread_mutex_lock(&mutex);
ready = true;
pthread_cond_signal(&cond);
pthread_cond_destroy(&cond); // ERROR!!!
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_mutex_lock(&mutex);
pthread_create(&thread, NULL, ThreadFunction, (void*) NULL);
while (!ready) { // to insure ourselves against spurious wakeups
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
pthread_join(thread, NULL);
pthread_mutex_destroy(&mutex);
std::cout << "finished" << std::endl;
return 0;
}
// CODE END
Martin Heller
|