Mailing Lists: Apple Mailing Lists

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

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.




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.