小编use*_*412的帖子

用短路操作代替链式ifs

这是一个非常简单的问题:有四个布尔函数:a(),b(),c()和d().我想继续按顺序调用它们,直到第一个返回true.而不是做传统的

if(!a()) {
    if(!b()) {
        if(!c()) {
            d();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

if(!a() && !b() && !c()) d();
Run Code Online (Sandbox Code Playgroud)

我想把这个表达写成一个短路的评估.

(a() || b() || c() || d());
Run Code Online (Sandbox Code Playgroud)

但我从未在C/C++代码中看到过这种测试.我错过了这种方法有什么问题吗?

谢谢.

c c++ short-circuiting

5
推荐指数
1
解决办法
238
查看次数

linux - 无法让 eventfd 与 epoll 一起工作

我正在编写一个基于 epoll 的简单服务器类。为了唤醒epoll_wait(),我决定使用eventfd。据说它更适合简单的事件通信,我同意这一点。所以我创建了我的活动并在上面放置了一个手表:

_epollfd = epoll_create1(0);
if (_epollfd == -1) throw ServerError("epoll_create");
_eventfd = eventfd(0, EFD_NONBLOCK);
epoll_event evnt = {0};
evnt.data.fd = _eventfd;
evnt.events = _events;
if (epoll_ctl(_epollfd, EPOLL_CTL_ADD, _eventfd, &evnt) == -1)
    throw ServerError("epoll_ctl(add)");
Run Code Online (Sandbox Code Playgroud)

稍后在消息等待循环中,在一个单独的线程上:

    int count = epoll_wait(_epollfd, evnts, EVENTS, -1);
    if (count == -1)
    {
        if (errno != EINTR)
        {
            perror("epoll_wait");
            return;
        }
    }

    for (int i = 0; i < count; ++i)
    {
        epoll_event & e = evnts[i];
        if (e.data.fd == _serverSock)
            connectionAccepted();
        else …
Run Code Online (Sandbox Code Playgroud)

linux events epoll

3
推荐指数
1
解决办法
8328
查看次数

标签 统计

c ×1

c++ ×1

epoll ×1

events ×1

linux ×1

short-circuiting ×1