Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a | both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
void pthread_testintr(void);
Allows the thread to solicit cancel requests at specific points within the current thread. You must have the cancelability state set to enabled (PTHREAD_INTR_ENABLE) for this function to have any effect.
pthread_testintr() returns no values.
There are no documented errno values. Use perror() or strerror() to determine the cause of the error.
⁄* CELEBP52 *⁄
#define _OPEN_THREADS
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
int thstatus;
char state[60] = "enable⁄controlled - initial default";
void * thfunc(void *voidptr)
{
int rc;
char *parmptr;
parmptr = voidptr;
printf("parm = %s.\n", parmptr);
if ( pthread_setintrtype(PTHREAD_INTR_CONTROLLED ) == -1 ) {
printf("set controlled failed. %s\n", strerror(errno));
thstatus = 103;
pthread_exit(&thstatus);
}
strcpy(state, "disable⁄controlled");
if ( pthread_setintr(PTHREAD_INTR_ENABLE) == -1 ) {
printf("set enable failed. %s\n", strerror(errno));
thstatus = 104;
pthread_exit(&thstatus);
}
strcpy(state, "enable⁄controlled");
strcat(state, " - pthread_testintr");
while(1) {
pthread_testintr();
sleep(1);
}
thstatus = 100;
pthread_exit(&thstatus);
}
main(int argc, char *argv[]) {
int rc;
pthread_attr_t attrarea;
pthread_t thid;
char parm[] = "abcdefghijklmnopqrstuvwxyz";
int *statptr;
if ( pthread_attr_init(&attrarea) == -1 ) {
printf("pthread_attr_init failed. %s\n", strerror(errno));
exit(1);
}
if ( pthread_create(&thid, &attrarea, thfunc, (void *)&parm) == -1) {
printf("pthread_create failed. %s\n", strerror(errno));
exit(2);
}
sleep(5);
if ( pthread_cancel(thid) == -1 ) {
printf("pthread_cancel failed. %s\n", strerror(errno));
exit(3);
}
if ( pthread_join(thid, (void **)&statptr)== -1 ) {
printf("pthread_join failed. %s\n", strerror(errno));
exit(4);
}
if ( statptr == (int *)-1 )
printf("thread was cancelled. state = %s.\n", state);
else
printf("thread was not cancelled. thstatus = %d.\n", *statptr);
exit(0);
}
parm = abcdefghijklmnopqrstuvwxyz.
thread was canceled. state = enable/controlled - pthread_testintr.