pthread_cancel 在solaris 下不起作用 [英] pthread_cancel don't work under solaris

查看:52
本文介绍了pthread_cancel 在solaris 下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 下,它按预期工作.

When I compile and run this under Solaris 10 (SunOS 5.10), it doesn't stop. But under Linux it works as intended.

推荐答案

对于 POSIX,printf(以及所有 stdio)可能是一个取消点.不是必须的.我怀疑 Solaris 只是没有选择让它成为一个.你有没有在这里尝试过像 sleep 这样的其他功能?

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?

如果你真的需要 printf 是可取消的,你可能需要实现你自己的类似 printf 的函数作为 dprintf 的包装器>,但是如果您依赖于 stdio 的内置锁定功能,那将不会那么好用..

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..

这篇关于pthread_cancel 在solaris 下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆