#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);
}
当我在Solaris 10(SunOS 5.10)下编译并运行它时,它不会停止。但是在Linux下它可以按预期工作。
答案 0 :(得分:4)
根据POSIX,printf
(和所有stdio)可能是取消点。不是必须的。我怀疑Solaris并没有选择让它成为一个。您是否尝试过其他功能,例如sleep
?
如果您真的需要printf
可取消,那么您可能需要实现自己的printf
- 类似函数作为dprintf
的包装,但这不会有效好吧,如果你依赖于stdio的内置锁定功能..