/***************************************************
*--------------------------------------------------
* Creat : 2001. 04. 10 (programed by Cori-Young )
* Site: http://www.byoneself.co.kr
* Update :
*--------------------------------------------------
* Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware: sun4u
* OS version: 5.7
* Processor type: sparc
* Hardware: SUNW,Ultra-60
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#define _MULTI_THREADED
static void checkResults(char *string, int rc) {
if (rc) {
printf("Error on : %s, rc=%d", string, rc);
exit(EXIT_FAILURE);
}
return;
}
void *threadfunc(void *parm)
{
printf("Entered secondary threadn");
while (1) {
printf("Secondary thread is loopingn");
pthread_testcancel();
sleep(1);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
printf("Entering testcasen");
printf("Create thread using the NULL attributesn");
rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)n", rc);
/* sleep() isn't a very robust way to wait for the thread */
sleep(2);
printf("Cancel the threadn");
rc = pthread_cancel(thread);
checkResults("pthread_cancel()n", rc);
/* sleep() isn't a very robust way to wait for the thread */
sleep(3);
printf("Main completedn");
return 0;
} |
|