public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* RE: pthread_cancel and pthread_mutex_lock
@ 2004-02-16 23:10 Simon Gerblich
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Gerblich @ 2004-02-16 23:10 UTC (permalink / raw)
  To: Rian Hunter; +Cc: pthreads-win32

Rian,

Have you worked out your problem?  It looks like no-one else has answered
you.  

I'd recommend you get the book "Programming with POSIX Threads" by Butenhof.
Also have a look at http://www.unix-systems.org/single_unix_specification/
for the pthread function specifications

A locked mutex should not act as a cancellation point.
I don't use and have not tried PTHREAD_CANCEL_ASYNCHRONOUS.  I use
PTHREAD_CANCEL_DEFERRED.
A mutex should be locked and then unlocked in the same thread normally.  I'm
not sure what PTHREAD_CANCEL_ASYNCHRONOUS should do.
I know that the pthreadsWin32 works fine for PTHREAD_CANCEL_DEFERRED.

If a thread locks a mutex and then calls a cancellation point the thread
should have a pthread_cancel_push() function to push a cancellation routine
onto the cancellation cleanup stack and the cancellation routine should
unlock the mutex.

Simon

> -----Original Message-----
> From:	Rian Hunter [SMTP:rian@thelaststop.net]
> Sent:	Wednesday, February 11, 2004 10:38 AM
> To:	pthreads-win32@sources.redhat.com
> Subject:	pthread_cancel and pthread_mutex_lock
> 
> Hello!
> 
> >From reading the mailing list archives i found out that at one point in 
> the pthreads-win32 library pthread_mutex_lock acted as a cancellation
> point, but since then that was removed. I have a thread that needs to be
> cancelled during a pthread_mutex_lock, so naturally i would have to set
> the cancel type to PTHREAD_CANCEL_ASYNCHRONOUS as per the new behavior.
> I'm not sure if pthread_mutex_lock can be asynchronously cancelled, but
> i'm having a problem with code similar to this:
> 
> ------------------
> #include <pthread.h>
> #include <stdio.h>
> 
> pthread_mutex_t inputLock;
> 
> void *t2(void *threadid) {
> 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
> 	printf("created \n");
> 
> 	/* waits to be cancelled here */
> 	pthread_mutex_lock(&inputLock);
> 	/* never gets here */
> 	printf("dead\n");
> 	return 0;
> }
> 
> int main() {
> 	pthread_t sender;
> 	int i;
> 
> 	/* initialize and lock mutex */
> 	pthread_mutex_init(&inputLock, NULL);
> 	pthread_mutex_lock(&inputLock);
> 
> 	pthread_create(&sender, NULL, t2, NULL);
> 
> 	/* psuedo sleep function */
> 	for(i = 0;i <10000000; i++) {}
> 
> 	printf("slept and thread created\n");
> 
> 	/* cancel thread and wait for termination */
> 	pthread_cancel(sender);
> 	pthread_join(sender, NULL);
> 
> 	/* check if thread was cancelled */
> 	printf("successfully canceled thread\n");
> 
> 	/* test lock for continued operation */
> 	pthread_mutex_unlock(&inputLock);
> 	pthread_mutex_lock(&inputLock);
> 
> 	/* signify correct execution */
> 	printf("all done\n");
> 	
> 	return 0;
> }
> 
> ----------
> 
> On FreeBSD the code executes correctly, at least i assume it is the
> correct behavior of a pthreads implementation. On windows it just
> freezes after it prints "created". I appreciate any help! thank you!
> -rian

^ permalink raw reply	[flat|nested] 2+ messages in thread

* pthread_cancel and pthread_mutex_lock
@ 2004-02-11  0:08 Rian Hunter
  0 siblings, 0 replies; 2+ messages in thread
From: Rian Hunter @ 2004-02-11  0:08 UTC (permalink / raw)
  To: pthreads-win32

Hello!

From reading the mailing list archives i found out that at one point in 
the pthreads-win32 library pthread_mutex_lock acted as a cancellation
point, but since then that was removed. I have a thread that needs to be
cancelled during a pthread_mutex_lock, so naturally i would have to set
the cancel type to PTHREAD_CANCEL_ASYNCHRONOUS as per the new behavior.
I'm not sure if pthread_mutex_lock can be asynchronously cancelled, but
i'm having a problem with code similar to this:

------------------
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t inputLock;

void *t2(void *threadid) {
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	printf("created \n");

	/* waits to be cancelled here */
	pthread_mutex_lock(&inputLock);
	/* never gets here */
	printf("dead\n");
	return 0;
}

int main() {
	pthread_t sender;
	int i;

	/* initialize and lock mutex */
	pthread_mutex_init(&inputLock, NULL);
	pthread_mutex_lock(&inputLock);

	pthread_create(&sender, NULL, t2, NULL);

	/* psuedo sleep function */
	for(i = 0;i <10000000; i++) {}

	printf("slept and thread created\n");

	/* cancel thread and wait for termination */
	pthread_cancel(sender);
	pthread_join(sender, NULL);

	/* check if thread was cancelled */
	printf("successfully canceled thread\n");

	/* test lock for continued operation */
	pthread_mutex_unlock(&inputLock);
	pthread_mutex_lock(&inputLock);

	/* signify correct execution */
	printf("all done\n");
	
	return 0;
}

----------

On FreeBSD the code executes correctly, at least i assume it is the
correct behavior of a pthreads implementation. On windows it just
freezes after it prints "created". I appreciate any help! thank you!
-rian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-02-16 23:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-16 23:10 pthread_cancel and pthread_mutex_lock Simon Gerblich
  -- strict thread matches above, loose matches on Subject: below --
2004-02-11  0:08 Rian Hunter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).