c - C 中的通用临时重试宏/函数 (TEMP_FAILURE_RETRY)

标签 c

我希望在 C 中实现通用的临时重试机制(仅重试一定次数)。我正在寻找与 GNU TEMP_FAILURE_RETRY 宏类似的功能。

我目前拥有的:

#define TEMP_RETRY_COUNT 10
#define TEMP_RETRY( exp )               \
  ({                                    \
    int _attemptc_ = TEMP_RETRY_COUNT;  \
    bool _resultb_;                     \
    while ( _attemptc_-- )              \
      if ( _resultb_ = exp ) break;     \
    _resultb_;                          \
  })

工作正常。我现在正试图抑制编译器警告的警告,并寻找更干净的东西:

bleh.c: In function ‘main’:
bleh.c:38:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if ( TEMP_RETRY( bleh() ) )
   ^
bleh.c:46:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   TEMP_RETRY( bleh() );

感谢您的任何回复!它不一定是一个宏。此外,可以假定 exp 返回 bool 值(或等效值)。

最佳答案

愚蠢的我:

#define TEMP_RETRY_COUNT 10
#define TEMP_RETRY( exp )               \
  ({                                    \
    int _attemptc_ = TEMP_RETRY_COUNT;  \
    bool _resultb_;                     \
    while ( _attemptc_-- )              \
      if ( (_resultb_ = exp) ) break;   \
    _resultb_;                          \
  })

关于c - C 中的通用临时重试宏/函数 (TEMP_FAILURE_RETRY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52102745/

相关文章:

c - 将结构写入二进制文件,然后读取文件并打印数据。

c - 我这样做是为了平衡方程的括号,但我认为这是错误的检查并纠正它

c - 如何在C中使用12位数字?

c - LLVM 编译器 2.0 错误?

c - 使用 read() 并将缓冲区放入字符串中

c++ - 读取没有正则表达式的行?

c - 非阻塞 BIO_do_connect 在没有互联网连接时阻塞

在 C 中计算 2^n 的数字和

c++ - 使用 c-preprocessor 自动生成函数转发器

c - 使用互斥锁按顺序同步线程