Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-Posix-秒等待/信号_C_Multithreading_Signals_Posix_Init - Fatal编程技术网

C-Posix-秒等待/信号

C-Posix-秒等待/信号,c,multithreading,signals,posix,init,C,Multithreading,Signals,Posix,Init,在纯C中,pthread_cond_wait和pthread_cond_signal/pthread_cond_destroy,pthread_cond_init在出现错误时设置errno?或者它们只是返回一个值!=0?来自: 请注意,pthreads函数不设置errno 特定函数的文档说明,如果出现故障,它们将返回一个错误号,以指示错误。因此,您只需检查函数返回的值。pthread\u cond\u wait(3)手册页: 成功完成后,应返回零值;否则,应返回错误号以指示错误 pthread_

在纯C中,
pthread_cond_wait
pthread_cond_signal
/
pthread_cond_destroy
pthread_cond_init
在出现错误时设置
errno
?或者它们只是返回一个值!=0?

来自:

请注意,pthreads函数不设置errno

特定函数的文档说明,如果出现故障,它们将返回一个错误号,以指示错误。因此,您只需检查函数返回的值。

pthread\u cond\u wait(3)手册页:

成功完成后,应返回零值;否则,应返回错误号以指示错误

pthread_cond_signal(3)手册页:

如果成功,pthread_cond_broadcast()和pthread_cond_signal()函数应返回零;否则,应返回错误号以指示错误

pthread_cond_destroy(3)手册页:

如果成功,pthread_cond_destroy()和pthread_cond_init()函数应返回零;否则,应返回错误号以指示错误

pthread_cond_init(3)手册页:

如果成功,pthread_cond_destroy()和pthread_cond_init()函数应返回零;否则,应返回错误号以指示错误

这意味着不会将错误号放入变量errno中,而是返回它。您可以使用
strerror
函数获取错误字符串

#include <stdio.h>
#include <pthread.h>

int ret;
if( (ret = pthread_cond_init( {some args} ) ) != 0 )
    fprintf(stderr, "%s\n", strerror(ret));
#包括
#包括
int ret;
if((ret=pthread_cond_init({some args}))!=0)
fprintf(stderr,“%s\n”,strerror(ret));

你看了吗?是的,我看了,它没有指定,只是说它返回一个值!=然后0,现在我的问题是,在运行它时,如何对它进行错误检查?我是否应该像(pthread_cond_wait(&cond,&mutex)!=0{peror…}?
int ret=pthread_cond_signal(…);switch(ret){case 0:break;case EINVAL:/*处理错误*/break;/*其他函数的其他错误*/}
。如果(pthread_cond_wait(&cond,&mutex)){handle error},这不是
也工作吗?如中所示,它还会运行函数吗?如果它返回0,它将继续运行,如果它返回!=0是否会跳转到if语句中@mchyes,但在本例中,您只知道发生了一个错误,而不知道是哪一个。是的,我已经意识到了这一点,我的问题是:
if(pthread_cond_wait(&cond,&mutex)){handle error}
这会运行wait函数,并且仅在返回错误时处理错误吗!=0? @Tsyvarevt谢谢,我的问题是它是否仍然会在if中运行函数,或者只是“测试”它。有时我会遇到这种derp时刻。哦,是的,
if
子句中的代码总是像普通的一样编译和执行。您可能会将其与
assert()
宏混淆,在未启用调试的情况下根本不会编译该条件。因为我不必检查错误类型,所以我可以像
if(function){handle error}
那样执行?由于函数仍然在if内执行?是,但是如果您想知道超时是否发生在等待过程中,则需要检查错误值。“除[ETIMEDOUT]的情况外,所有这些错误检查应在函数处理开始时立即执行,并应在修改互斥体指定的互斥体状态或cond指定的条件变量之前导致错误返回。”