Ich habe ein Problem mit Pthread Abbruch und mit Cleanup-Handler. In der POSIX-Bibliothek gibt es zwei Funktionen: pthread_cleanup_push und pthread_cleanup_pop. Das Problem ist, dass sie keine Funktionen sind, sondern Makros! Darüber hinaus scheint es, dass sie in Paaren platziert werden müssen, d. H. Für jeden Push, muss auch Pop sein. Aber ich finde es etwas merkwürdig.pthread_cancel() und Verwendung von Bereinigungshandlern.
Angenommen, ich solche Code-Struktur haben:
{
pthread_mutex_lock(&mutex);
pthread_cleanup_push(cleanup_mutex, &mutex);
// any code that can be cancellation point
while(/*some condition *) {
// any code that cancellation point
// 1. signaled -> thread given mutex
// 2. timeout -> thread given mutex
// 3. canceled -> thread given mutex and than cleanup handlers
if(pthread_cond_timedwait(&cond, &mutex, &abstimeout) == ETIMEDOUT) {
/*** HERE SHOULD BE pthread_cleanup_pop(0)! But it cannot be placed! case 2)***/
pthread_mutex_unlock(&mutex);
return NULL;
}
}
// any code that can be cancellation point
// consume something
// any code that can be cancellation point
pthread_cleanup_pop(0); // takes care of case 1)
pthread_mutex_unlock(&mutex);
return something;
}
Gibt es da eine Frage? – EOF