问题
#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.
回答1:
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 like sleep
here?
If you really need printf
to be cancellable, you'll probably need to implement your own printf
-like function as a wrapper for dprintf
, but that won't work so well if you're depending on the builtin locking functionality of stdio..
来源:https://stackoverflow.com/questions/12455815/pthread-cancel-dont-work-under-solaris