Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: POSIX threads - pthread_cancel



Hi Mukesh,

We have been porting to Mac OS X multiple Pthread applications and have had
similar problems. I have been looking through the Pthread source code from
the Darwin site. It appears that the only cancellation points are
pthread_testcancel(), pthread_setcancelstate(), and pthread_setcanceltype().
All of the other cancellation points that are documented in Mac OS man pages
and others that are also customary/required by the POSIX standard are not
implemented to support cancellation. Also, I believe that
pthread_setcanceltype() and pthread_setcancelstate() are not supposed to be
deferred cancellation points. That will also cause some problems if you
rely on adherence to the POSIX standard for cancellation behavior.
Asynchronous cancellation is not supported either.

In Linux, nanosleep is a cancellation point, so you were probably receiving
the cancellation there. When you run the thread detached in Mac OS, it
probably works just due to timing. Based on whether it is detached or not,
you may be receiving the cancellation in the pthread_testcancel() or one of
the pthread_setcancelXXX() routines. But once you are in the nanosleep, you
will never receive the cancellation.

We are looking into some modifying the Pthread source to give us some more
support for cancellation in I/O and sleep functions. We will look into
posting it if the Darwin group wants it.

Hope this helps....

Mark




-----Original Message-----
From: email@hidden
[mailto:email@hidden]On Behalf Of mukesh bafna
Sent: Friday, March 12, 2004 4:26 AM
To: email@hidden
Subject: POSIX threads - pthread_cancel


Hi all,
I am having problem with pthread_cancel on mac os x. The program given
below works properly
on linux but when i tried the same program on mac os x but it didn't work
properly. It seems
pthread_cancel isn't functioning properly on mac os x.
Strangly when i set the thread attribute detach state to true
pthread_cancel seems to work fine
can u tell me the problem with pthread_cancel ?

Thank you,
Mukesh

*********************************** Start ThreadCancel.cpp
***************************************
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#define SUCCESS 0
#define TRUE 1
// function prototype
void * thread_run(void *); // thread run function
// Thread Cancel program testing
int main(int argc,char * argv[])
{
pthread_t thread;
int retval;
// Create a thread
fprintf(stdout,"\nCreating thread using pthread_create...\n");
retval = pthread_create(&thread,NULL,thread_run,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread creation failed...\n");
exit(EXIT_FAILURE);
}
sleep(3);
fprintf(stdout,"Cancelling thread...\n");
retval = pthread_cancel(thread);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancelling failed...\n");
exit(EXIT_FAILURE);
}
fprintf(stdout,"Waiting for thread to finish...\n");
retval = pthread_join(thread,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread creation failed...\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
} // end thread_main
// Thread task
void * thread_run(void * argument )
{
int retval;
// Set thread cancel type
fprintf(stdout,"Setting pthread_setcancelstate...\n");
retval = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancel state failed...\n");
exit(EXIT_FAILURE);
}
// Set thread cancel type
fprintf(stdout,"Setting pthread_setcanceltype...\n");
retval = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancel type failed...\n");
exit(EXIT_FAILURE);
}
// Set thread cancellation point
pthread_testcancel();
// Setting up infinite loop
while ( TRUE )
{
struct timespec wait;
wait.tv_sec = 2;
wait.tv_nsec = 0;
fprintf(stderr,"Waiting in loop...\n");
retval = nanosleep(&wait,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"nanosleep failed...\n");
exit(EXIT_FAILURE);
}
}
pthread_exit(NULL);
} // end thread_run
*********************************** End ThreadCancel.cpp
***************************************
*********************************** Start ThreadCancelDetached.cpp
***************************************
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#define SUCCESS 0
#define TRUE 1
// function prototype
void * thread_run(void *); // thread run function
// Thread Cancel program testing
int main(int argc,char * argv[])
{
pthread_t thread;
pthread_attr_t attr;
int retval;
fprintf(stdout,"set thread attr init...\n");
retval = pthread_attr_init(&attr);
if ( retval != SUCCESS )
{
fprintf(stderr,"\nThread attr init error...\n");
exit(EXIT_FAILURE);
}
fprintf(stdout,"set pthread detach state...\n");
if ( retval =
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED))
if ( retval != SUCCESS )
{
fprintf(stderr,"\nThread set detach state...\n");
exit(EXIT_FAILURE);
}
// Create a thread
fprintf(stdout,"\nCreating thread using pthread_create...\n");
retval = pthread_create(&thread,&attr,thread_run,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread creation failed...\n");
exit(EXIT_FAILURE);
}
sleep(3);
fprintf(stdout,"Cancelling thread...\n");
retval = pthread_cancel(thread);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancelling failed...\n");
exit(EXIT_FAILURE);
}
fprintf(stdout,"Destroy thread attr...\n");
pthread_attr_destroy(&attr);
exit(EXIT_SUCCESS);
} // end thread_main
// Thread task
void * thread_run(void * argument )
{
int retval;
// Set thread cancel type
fprintf(stdout,"Setting pthread_setcancelstate...\n");
retval = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancel state failed...\n");
exit(EXIT_FAILURE);
}
// Set thread cancel type
fprintf(stdout,"Setting pthread_setcanceltype...\n");
retval = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"thread cancel type failed...\n");
exit(EXIT_FAILURE);
}
// Set thread cancellation point
pthread_testcancel();
// Setting up infinite loop
while ( TRUE )
{
struct timespec wait;
wait.tv_sec = 2;
wait.tv_nsec = 0;
fprintf(stderr,"Waiting in loop...\n");
retval = nanosleep(&wait,NULL);
if ( retval != SUCCESS )
{
fprintf(stderr,"nanosleep failed...\n");
exit(EXIT_FAILURE);
}
}
pthread_exit(NULL);
} // end thread_run
*********************************** End ThreadCancelDetached.cpp
****************************
Yahoo! Search - Find what youre looking for faster.
_______________________________________________
darwin-userlevel mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/darwin-userlevel
Do not post admin requests to the list. They will be ignored.
_______________________________________________
darwin-userlevel mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-userlevel
Do not post admin requests to the list. They will be ignored.


References: 
 >POSIX threads - pthread_cancel (From: mukesh bafna <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.