Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jul 2004 07:14:59 +0200
From:      "Petr Holub" <hopet@ics.muni.cz>
To:        "Daniel Eischen" <eischen@vigrid.com>
Cc:        threads@freebsd.org
Subject:   RE: sem_wait as cancellation point
Message-ID:  <007d01c464aa$840deea0$b736fb93@KLOBOUCEK>
In-Reply-To: <Pine.GSO.4.10.10407070818110.11953-100000@pcnet5.pcnet.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> 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@cesnet.cz               phone: +420-549493944
                                             fax: +420-541212747
                                       e-mail: hopet@ics.muni.cz

 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?007d01c464aa$840deea0$b736fb93>