pthread_cancel don't work under solaris

这一生的挚爱 提交于 2019-12-11 09:36:43

问题


#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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!