Re: mutex PTHREAD_MUTEX_ERRORCHECK and pthread_cancel issues
Re: mutex PTHREAD_MUTEX_ERRORCHECK and pthread_cancel issues
- Subject: Re: mutex PTHREAD_MUTEX_ERRORCHECK and pthread_cancel issues
- From: Stéphane Letz <email@hidden>
- Date: Fri, 13 May 2005 09:38:22 +0200
Le 12 mai 05, à 22:27, Matt Watson a écrit :
On May 12, 2005, at 11:25 AM, Stéphane Letz wrote:
But after a pthread_cancel() has been called, the
__pthread_destructor code is never called. Is there any known issue
in using the pthread_key_create system?
Are you sure your code has reached a cancellation point?
The following works fine for me on Tiger:
% cat test.c
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
static pthread_key_t k;
void *
func(void *arg)
{
assert(!pthread_setspecific(k, arg));
assert(!pthread_mutex_lock(&m));
while (1) {
assert(!pthread_cond_wait(&c, &m));
}
/* NOT REACHED */
assert(!pthread_mutex_unlock(&m));
return NULL;
}
void cleanup_func(void *arg)
{
printf("cleanup called with %p\n", arg);
}
int
main(void)
{
pthread_t t;
int i = 42;
void *ret;
assert(!pthread_key_create(&k, cleanup_func));
printf("creating a thread with arg %p\n", &i);
assert(!pthread_create(&t, NULL, func, &i));
assert(!pthread_cancel(t));
assert(!pthread_join(t, &ret));
assert(PTHREAD_CANCELED == ret);
return 0;
}
% make test CFLAGS='-Os -Wall -W -D_APPLE_C_SOURCE'
cc -Os -Wall -W -D_APPLE_C_SOURCE test.c -o test
% ./test
creating a thread with arg 0xbffffc7c
cleanup called with 0xbffffc7c
Thanks. It works OK.
But :
(1) using pthread_testcancel instead pthread_cond_wait in the thread
loop of does not work.... Why?
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
static pthread_key_t k;
void *
func(void *arg)
{
assert(!pthread_setspecific(k, arg));
assert(!pthread_mutex_lock(&m));
while (1) {
pthread_testcancel();
}
/* NOT REACHED */
assert(!pthread_mutex_unlock(&m));
return NULL;
}
void cleanup_func(void *arg)
{
printf("cleanup called with %p\n", arg);
}
int
main(void)
{
pthread_t t;
int i = 42;
void *ret;
assert(!pthread_key_create(&k, cleanup_func));
printf("creating a thread with arg %p\n", &i);
assert(!pthread_create(&t, NULL, func, &i));
sleep(1);
printf("pthread_cancel 0\n");
assert(!pthread_cancel(t));
printf("pthread_cancel 1\n");
printf("pthread_join 0\n");
assert(!pthread_join(t, &ret));
printf("pthread_join 1 %x\n",ret);
assert(PTHREAD_CANCELED == ret);
return 0;
}
./test
creating a thread with arg 0xbffffc20
pthread_cancel 0
pthread_cancel 1
pthread_join 0
....
(1) Using an asynchronous cancellation mode does not work also :
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
static pthread_key_t k;
void *
func(void *arg)
{
assert(!pthread_setspecific(k, arg));
assert(!pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
assert(!pthread_mutex_lock(&m));
while (1) {
pthread_testcancel();
}
/* NOT REACHED */
assert(!pthread_mutex_unlock(&m));
return NULL;
}
void cleanup_func(void *arg)
{
printf("cleanup called with %p\n", arg);
}
int
main(void)
{
pthread_t t;
int i = 42;
void *ret;
assert(!pthread_key_create(&k, cleanup_func));
printf("creating a thread with arg %p\n", &i);
assert(!pthread_create(&t, NULL, func, &i));
sleep(1);
printf("pthread_cancel 0\n");
assert(!pthread_cancel(t));
printf("pthread_cancel 1\n");
printf("pthread_join 0\n");
assert(!pthread_join(t, &ret));
printf("pthread_join 1 %x\n",ret);
assert(PTHREAD_CANCELED == ret);
return 0;
}
./test
creating a thread with arg 0xbffffc20
pthread_cancel 0
pthread_cancel 1
pthread_join 0
....
Is pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) supposed to
work on Tiger?
Thanks
Stephane Letz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden