#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
char a[]="Hello";
void * thread_body(void * param) {
while(1)
printf("%s\n", param);
}
int main(int argc, char *argv[]) {
pthread_t threadHello;
int code;
pthread_create(&threadHello, NULL, thread_body, a);
pthread_cancel(threadHello);
pthread_exit(0);
}
When I compile and run this under Solaris 10 (SunOS 5.10), it doesn’t stop. But under Linux it works as intended.
Per POSIX,
printf(and all of stdio) may be a cancellation point. It is not required to be. I suspect Solaris just doesn’t choose to make it one. Have you tried another function likesleephere?If you really need
printfto be cancellable, you’ll probably need to implement your ownprintf-like function as a wrapper fordprintf, but that won’t work so well if you’re depending on the builtin locking functionality of stdio..