c语言如何实现事件循环-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

c语言如何实现事件循环

a123456678 2016-06-07 17:59:51 2514

想用c语言实现类似glib mainloop那样的事件循环机制,求思路或方向,谢谢各位大神!

c语言c实现 esxi事件
分享到
取消 提交回答
全部回答(7)
  • 游客x4uu57rrcwjuo
    2020-03-17 16:59:18

    while (true) sleep(0);

    0 0
  • 游客duzwdtzqsaq3i
    2020-03-17 10:39:32

    大道至简,用while

    0 0
  • 一生有你llx
    2019-07-17 19:30:37

    struct pollfd fds[2];
    int timeout_msecs = 500;
    int ret;
    int i;
    / Open STREAMS device. /
    fds[0].fd = open("/dev/dev0", ...);
    fds[1].fd = open("/dev/dev1", ...);
    fds[0].events = POLLOUT | POLLWRBAND;
    fds[1].events = POLLOUT | POLLWRBAND;
    while(1) {

    ret = poll(fds, 2, timeout_msecs);
    if (ret > 0) {

    / An event on one of the fds has occurred. /
    for (i=0; i<2; i++) {
    if (fds[i].revents & POLLWRBAND) {
    / Priority data may be written on device number i. /
    ...
    }
    if (fds[i].revents & POLLOUT) {
    / Data may be written on device number i. /
    ...
    }
    if (fds[i].revents & POLLHUP) {
    / A hangup has occurred on device number i. /
    ...
    }
    }
    }
    }

    0 0
  • nothingfinal
    2019-07-17 19:30:36

    struct pollfd fds[2];
    int timeout_msecs = 500;
    int ret;
    int i;
    / Open STREAMS device. /
    fds[0].fd = open("/dev/dev0", ...);
    fds[1].fd = open("/dev/dev1", ...);
    fds[0].events = POLLOUT | POLLWRBAND;
    fds[1].events = POLLOUT | POLLWRBAND;
    while(1) {

    ret = poll(fds, 2, timeout_msecs);
    if (ret > 0) {

    / An event on one of the fds has occurred. /
    for (i=0; i<2; i++) {
    if (fds[i].revents & POLLWRBAND) {
    / Priority data may be written on device number i. /
    ...
    }
    if (fds[i].revents & POLLOUT) {
    / Data may be written on device number i. /
    ...
    }
    if (fds[i].revents & POLLHUP) {
    / A hangup has occurred on device number i. /
    ...
    }
    }
    }
    }

    0 0
  • xumaojun
    2019-07-17 19:30:36

    struct pollfd fds[2];
    int timeout_msecs = 500;
    int ret;
    int i;
    / Open STREAMS device. /
    fds[0].fd = open("/dev/dev0", ...);
    fds[1].fd = open("/dev/dev1", ...);
    fds[0].events = POLLOUT | POLLWRBAND;
    fds[1].events = POLLOUT | POLLWRBAND;
    while(1) {

    ret = poll(fds, 2, timeout_msecs);
    if (ret > 0) {

    /* An event on one of the fds has occurred. */  
    for (i=0; i<2; i++) {  
    if (fds[i].revents & POLLWRBAND) {  
    /* Priority data may be written on device number i. */  
    ...  
    }  
    if (fds[i].revents & POLLOUT) {  
    /* Data may be written on device number i. */  
    ...  
    }  
    if (fds[i].revents & POLLHUP) {  
    /* A hangup has occurred on device number i. */  
    ...  
    }  
    }  

    }
    }

    0 0
  • calm86104
    2019-07-17 19:30:36

    你首先要确定是哪类的 EVENT。
    好吧,我来做一个通用的回答。

    通畅,在 linux 端,我们会使用 epoll 函数来做事件循环:
    如下举例:

    image

    0 0
  • a123456678
    2019-07-17 19:30:36

    一个很 ugly (但有效)的解决方案:
    定义一个 struct Event 的队列

    struct Event event;
    while (1)
    {
        if (empty(event_queue))
            sleep(WAITING);
        else {
            event = get_first_element(event_queue);
            remove_first_element(event_queue);
            process...
        }
    }
    0 0
滑动查看更多
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题