This is the mail archive of the
glibc-bugs@sources.redhat.com
mailing list for the glibc project.
cleanup handlers and sem_wait ....
- From: webmyster at laposte dot net
- To: glibc-bugs at sources dot redhat dot com
- Date: Tue, 22 Mar 2005 15:03:14 +0100
- Subject: cleanup handlers and sem_wait ....
I have a problem with cleanup handlers and sem_wait with the small
program attached under linux.
On a first computer (glibc 2.3.4, gcc 3.3.3, kernel 2.6.8.1), when I
compile it with g++, although the thread seems to close, the cleanup
handler is not call. When I compile the programm with gcc, no problem
occurs : the cleanup handler is call and the thread stop.
On a second computer (redhat 9.0 : glibc 2.3.2, gcc 3.2.2, kernel
2.4.20), whatever compiler I use (gcc and g++), when I run this program,
it freezes in the pthread_join. I've seen that previous versions of
glibc made sem_wait function not a cancelation point. So could you
confirm that this version is one of those ?
Have I done a mistake in my program ?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
void testCancel(void *data) {
fprintf(stderr, "*********************************************************************\n");
fprintf(stderr, "testCancel !\n");
fprintf(stderr, "*********************************************************************\n");
}
void * testsem (void *data) {
pthread_cleanup_push(testCancel, NULL);
sem_t my_semaphore;
sem_init(&my_semaphore, 0, 0);
sem_wait(&my_semaphore);
fprintf(stderr, "Got over sem_wait - strange.\n");
pthread_cleanup_pop(0);
}
int main() {
pthread_t test_thread;
void *thread_result;
fprintf(stderr, "Creating thread.\n");
pthread_create(&test_thread, NULL, testsem, (void *) NULL);
fprintf(stderr, "Sleeping for 3 secs.\n");
sleep(3);
fprintf(stderr, "Trying to cancel the thread.\n");
pthread_cancel(test_thread);
fprintf(stderr, "Trying to join the thread.\n");
pthread_join(test_thread, &thread_result);
fprintf(stderr, "Finished.\n");
return(EXIT_SUCCESS);
}