pthread_cancel problem on MAC OSX
pthread_cancel problem on MAC OSX
- Subject: pthread_cancel problem on MAC OSX
- From: Girish <email@hidden>
- Date: Sun, 4 Jan 2004 22:43:09 -0800 (PST)
Hi Darwin Devloper,
I am doing some assignments related to thread in user
space.After doing lots of R & D, I came to know that
"pthread_cancel()" is not working properly.The same
program is working fine on Linux.
Program is as follows...
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define SUCCESS 0
#define TRUE 1
// Thread function declaration
void * run(void *);
// Program to check for pthread cancel
int main(int argc,char * argv[])
{
pthread_t thread;
int retval;
// Create thread which will fall in infinite
loop
retval =
pthread_create(&thread,NULL,run,NULL);
if ( retval != SUCCESS )
{
perror("Thread Creation Failed...\n");
exit(EXIT_FAILURE);
}
sleep(3);
// Cancel Thread
fprintf(stdout,"Canceling thread...\n");
retval = pthread_cancel(thread);
if ( retval != SUCCESS )
{
perror("Thread Caneclation
Failed...\n");
exit(EXIT_FAILURE);
}
// Wait for Thread to join
fprintf(stdout,"Waiting for Thread to
Finish...\n");
retval = pthread_join(thread,NULL);
if ( retval != SUCCESS )
{
perror("Thread Joining Failed...\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
// Thread function
void * run(void * arg)
{
int retval;
// Set thread cancel state
retval =
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
if ( retval != SUCCESS )
{
perror("Thread pthread_setcancelstate
Failed...\n");
exit(EXIT_FAILURE);
}
// Set thread cancel type
retval =
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
if ( retval != SUCCESS )
{
perror("Thread pthread_setcanceltype
failed...");
exit(EXIT_FAILURE);
}
// Infinite loop
fprintf(stdout,"Thread Function is
running...\n");
while ( TRUE )
{
sleep(1);
}
}
Regards
Girish V. Dudhe
(S P Software,Pune,India)
__________________________________
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003
_______________________________________________
darwin-kernel mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/darwin-kernel
Do not post admin requests to the list. They will be ignored.