【问题标题】:pthread_cancel don't work under solarispthread_cancel 在 solaris 下不起作用
【发布时间】:2012-09-17 08:47:07
【问题描述】:
#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 下它可以正常工作。

【问题讨论】:

  • pthread_cancel() 返回什么值?
  • 你有没有read the manual检查过目标线程的取消状态和类型?
  • @kerrek-sb 是的,我阅读了 linux 和 solaris 下的手册页。
  • @KingsIndian 没有任何改变
  • @AlexeyMatveev 你检查过这些函数的返回值了吗?他们都成功了?

标签: c pthreads solaris


【解决方案1】:

根据 POSIX,printf(以及所有 stdio)可能是取消点。它不是必需的。我怀疑 Solaris 只是没有选择使其成为一体。您是否在这里尝试过像sleep 这样的其他功能?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 2011-05-06
    • 1970-01-01
    • 2017-06-07
    • 2016-06-25
    • 2014-04-07
    • 2014-04-07
    • 2014-11-18
    相关资源
    最近更新 更多