开发者社区> 问答> 正文

c语言如何实现事件循环

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

展开
收起
a123456678 2016-06-07 17:59:51 3890 0
7 条回答
写回答
取消 提交回答
  • while (true) sleep(0);

    2020-03-17 16:59:18
    赞同 展开评论 打赏
  • 大道至简,用while

    2020-03-17 10:39:32
    赞同 展开评论 打赏
  • 专注物联网

    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. /
    ...
    }
    }
    }
    }

    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. /
    ...
    }
    }
    }
    }

    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. */  
    ...  
    }  
    }  

    }
    }

    2019-07-17 19:30:36
    赞同 展开评论 打赏
  • 你首先要确定是哪类的 EVENT。
    好吧,我来做一个通用的回答。

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

    image

    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...
        }
    }
    2019-07-17 19:30:36
    赞同 展开评论 打赏
滑动查看更多
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载