【问题标题】:exchange globale variable between worker thread epolled and a config thread在工作线程 epolled 和配置线程之间交换全局变量
【发布时间】:2015-11-20 15:29:00
【问题描述】:

我有一个工作线程,它在 fds 和计时器上执行 epoll_wait,其配置存储在全局上下文结构 (fdToRead) 中。 其他一些线程会更改此全局结构。

这是原理图

工作线程

struct epoll_event ev
while(1){
    epoll_wait(&ev)
    // call to changeFd(path) done here from other thread
    get_mutex
    read(ev.data.fd)//this fd was just closed or re-affect to another file !!!
    ...
    put_mutex
}

来自任何线程的函数调用

changeFd(path){
  get_mutex
  close(fdToRead)
  fdToRead=openSocket(path)
  epoll_add(fdToRead)
  put_mutex
}

问题是如果在 epoll_wait 之后 get_mutex 之前调用 changeFd,我会在 fd 上收到关闭或重新影响另一个“内核中打开文件描述符”的事件。

我可以通过哪种方式做到这一点? 哪个模板模式?

是否可以做类似“epoll_wait 中的get_mutex”之类的事情? 或者,“我可以在 epoll_wait 调用中阻塞”工作线程吗?

【问题讨论】:

    标签: c linux pthreads mutex epoll


    【解决方案1】:

    您可以使用accept() 函数,它允许您在套接字上接受新连接,您的代码可能如下所示:

    mutex_lock(...)
    epoll_wait(...);
    accept(...);
    mutex_unlock(...);
    

    【讨论】:

    • 抱歉,这不能解决我的问题,我知道如何使用互斥锁。问题是同时使用epoll和mutex
    • 不行,epoll_wait会等待很长时间,所以config会等待mutex很长时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多