【发布时间】:2011-02-26 20:06:29
【问题描述】:
背景:这是this thread 的后续问题,关于在 C++ (Linux/GCC) 中处理系统调用的 EINTR。无论我是否打算分析我的应用程序,似乎我都应该将系统调用设置为 errno 到 EINTR 作为特例。关于goto的使用,有many、many、many意见。
我的问题:是系统调用设置errno 到EINTR goto 被认为是名义上的情况?如果不是,那么您建议如何转换以下代码来处理EINTR?
if ( ( sock_fd = ::socket( domain, type, protocol ) ) < 0 ) {
throw SocketException( "Socket::Socket() -> ::socket()", errno );
}
提前致谢!
干杯,
-克里斯
更新:根据以下答案,我最终编写了以下宏:
#define SOCK_SYSCALL_TRY(call,error) \
while ( (call) < 0 ) { \
switch ( errno ) { \
case EINTR: \
continue; \
default: \
throw SocketException( (error), errno ); \
} \
} \
这是用来把我原来的sn-p转换成这个例子的:
SOCK_SYSCALL_TRY( sock_fd = ::socket( domain, type, protocol ), "Socket::Socket() -> ::socket()" )
希望这对其他人有帮助!
【问题讨论】:
-
克里斯,得到这本书:unpbook.com - 你会很高兴你做到了。源代码在线unpbook.com/src.html - 有关如何处理 EINTR 的示例,请参见那里。
-
我从没想过使用 while/continue - 好主意!
标签: c++ goto system-calls eintr