sem_wait as cancellation point

Petr Holub hopet at ics.muni.cz
Wed Jul 7 22:15:04 PDT 2004


> sem_wait() is a cancellation point in libpthread.

No, it is not - at least according to my experience of 5.2.1-RELEASE-p9.
When I use following snipplet of code, it keeps hanging in the pthread_join:

----------------------------------------------------------------
/* Example to demonstate non-cancellable behavior of sem_wait in phtread
 * environment. I've tested it on FreeBSD 5.2.1-RELEASE-p9 and the program
 * was compiled using:
 * cc -D_THREAD_SAFE -o sem_wait sem_wait.c -pthread -lc_r
 */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

pthread_t test_thread;
sem_t my_semaphore;

void * testsem (void *data) {
    printf("Initing semaphore.\n");
	sem_init(&my_semaphore, 0, 0);
    printf("Posting semaphore.\n");
	sem_post(&my_semaphore);
    printf("Waiting for semaphore (this should pass).\n");
	sem_wait(&my_semaphore);
    printf("Waiting for semaphore (this should hang).\n");
	sem_wait(&my_semaphore);
    printf("Got over sem_wait - strange.\n");
}

int main() {
	void *thread_result;

    printf("Creating thread.\n");
	pthread_create(&test_thread, NULL, testsem, (void *) NULL);
    printf("Sleeping for 5 secs.\n");
	sleep(3);
    printf("Trying to cancel the thread.\n");
	pthread_cancel(test_thread);
	sleep(1);
    printf("Trying to join the thread.\n");
	pthread_join(test_thread, &thread_result);

    printf("Finished.\n");
	return(EXIT_SUCCESS);
}
----------------------------------------------------------------

Am I doing something wrong?

Thanks,
Petr

================================================================
                            Petr Holub
CESNET z.s.p.o.                       Supercomputing Center Brno
Zikova 4                             Institute of Compt. Science
162 00 Praha 6, CZ                            Masaryk University
Czech Republic                     Botanicka 68a, 60200 Brno, CZ 
e-mail: Petr.Holub at cesnet.cz               phone: +420-549493944
                                             fax: +420-541212747
                                       e-mail: hopet at ics.muni.cz

 



More information about the freebsd-threads mailing list