gpt4 book ai didi

c - pthread_cancel 返回 EINPROGRESS

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:22 26 4
gpt4 key购买 nike

我现在正在维护一些代码,它使用 pthread_create() 创建一个 pthread。此线程还在创建后不久调用 pthread_detach(pthread_self());。此外,它使用 pthread_cleanup_push() 来推送清理处理程序。此线程的唯一取消点是在线程主循环结束时调用 pthread_testcancel()

我现在明白了,在某个时刻 pthread_cancel() 在此线程上被调用并且(几乎)总是返回一个非零值。检查 errno 结果为 EINPROGRESS。这到底是什么意思?因为手册页指出

On success, pthread_cancel() returns 0

但是,它也提到,

that cancellation will be delayed until the thread next calls a function that is a cancellation point.

因此,如果返回值非零且 errno 等于 EINPROGRESS 意味着取消请求已排队,但尚未作出 react ,我希望情况总是如此。但如果返回值为 0 表示成功,那么它一定意味着不同的东西。

在这种情况下,EINPROGRESS 是什么意思?我如何确定线程已正确取消或已终止?


我在 VM 中运行带有 libpthread-2.13 作为 libc6:amd64 2.13-38+deb7u1 一部分的 Debian 7.4。

最佳答案

I now see, that at some point pthread_cancel() gets called on this thread and (almost) always returns a nonzero value. Checking errno results in EINPROGRESS.

pthread* 函数族不设置errno,而是将错误代码作为函数值返回。

因此,不仅要针对 0 测试调用 pthread_cancel() 的结果,还要实际将返回值解释为您“正常”在 中找到的值>errno,这是 E* 错误值之一。

int result = pthread_cancel(<some-pthread>);
if (0 != result)
{
fprintf(stderr, "pthread_cancel() failed with error #%d: '%s'\n", result, strerror(result)); /* Better use strerror_r() in a multithreaded enviroment. */
}

顺便说一句:检查您似乎正在使用的 libc 的源代码不会显示任何 pthread_cancel() 返回 EINPROGRESS 的提示。

关于c - pthread_cancel 返回 EINPROGRESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23225449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com