我应该设置errno吗?(Should I set errno?)

我正在写一个模块,导出类似于send和recv的接口。

由于这些函数应该分别返回发送和接收的字节数,所以我不能像通常那样进行适当的错误管理(即使用枚举和返回助记符值)。

在这样的情况下,我应该设置errno作为标准库吗? 如果是这样,由于errno是线程特定的,有没有特别的写法,或者我可以简单地给它赋值?

编辑:试验它我注意到,通过赋值设置errno正在工作。 仍然:这是安全和便携的任何系统?

I'm writing a module which exports an interface similar to send and recv.

Since those functions are supposed to return respectively the number of sent and received bytes, I cannot do proper error management as I would do normally (i.e. using enumeratives and returning mnemonic values).

In a situation like this should I set errno as the standard library does? If so, since errno is thread specific, is there a particular way of writing on it, or can I simply assign a value to it?

Edit: experimenting it I noticed that setting errno by assignment is working. Still: is this safe and portable for any system?

最满意答案

这有点旧 ,但errno - 手册第3节说,你可以直接分配给它,即使它是一个宏,它将是线程本地

This is a bit old, but errno - manual section 3 says that you can directly assign to it, even though it is a macro, and it will be thread local

我应该设置errno吗?(Should I set errno?)

我正在写一个模块,导出类似于send和recv的接口。

由于这些函数应该分别返回发送和接收的字节数,所以我不能像通常那样进行适当的错误管理(即使用枚举和返回助记符值)。

在这样的情况下,我应该设置errno作为标准库吗? 如果是这样,由于errno是线程特定的,有没有特别的写法,或者我可以简单地给它赋值?

编辑:试验它我注意到,通过赋值设置errno正在工作。 仍然:这是安全和便携的任何系统?

I'm writing a module which exports an interface similar to send and recv.

Since those functions are supposed to return respectively the number of sent and received bytes, I cannot do proper error management as I would do normally (i.e. using enumeratives and returning mnemonic values).

In a situation like this should I set errno as the standard library does? If so, since errno is thread specific, is there a particular way of writing on it, or can I simply assign a value to it?

Edit: experimenting it I noticed that setting errno by assignment is working. Still: is this safe and portable for any system?

最满意答案

这有点旧 ,但errno - 手册第3节说,你可以直接分配给它,即使它是一个宏,它将是线程本地

This is a bit old, but errno - manual section 3 says that you can directly assign to it, even though it is a macro, and it will be thread local