简体   繁体   English

如何在任何事件发生之前唤醒epoll_wait()?

[英]How to wake up epoll_wait() before any event happened?

I have a Connection object that represents a connection managed by epoll . 我有一个Connection对象,它表示由epoll管理的连接。 On events, the Connection::eventWrite() or Connection::eventRead() are invoked. 在事件上,将调用Connection::eventWrite()Connection::eventRead() Connection is persistent (exists always). 连接是持久的(始终存在)。

I need to send data via that Connection from another thread, but don't want to access the object Connection directly, because I dont want to solve multithreading issues inside the Connection object ( Connection is accessed from epoll -thread and from "sending thread"). 我需要从另一个线程通过该Connection发送数据,但是不想直接访问对象Connection ,因为我不想解决Connection对象内部的多线程问题( Connection是从epoll -thread和“发送线程”访问的) )。 Instead, I want to tell epoll -thread: "wake up and do something with Connection " -- epoll_wait() should return and my code will find out that a CUSTOM EVENT is occured (and, for example, some function should be called, where the desired access to Connection will be performed (assign sending data to object, calling writeEvent() ), then epoll_wait() -loop will be continued. 相反,我想告诉epoll -thread:“醒来并用Connection做一些事情” epoll_wait()应该返回,我的代码将发现发生了CUSTOM EVENT (例如,应调用某些函数,在其中将执行对Connection的所需访问(将数据发送给对象,调用writeEvent() ),然后epoll_wait()将继续。

What kind of solutions exists for waking up a epoll_wait() and which one is faster? 存在哪种唤醒epoll_wait()的解决方案,哪种解决方案更快? For now, I came up with creating a pipe (handled by the same epoll ) and writing a byte 1 to it. 现在,我想到创建一个pipe (由相同的epoll处理)并向其写入字节1 What else IPC exists which can be expressed via int "epollable" file descriptor? 还可以通过int “ epollable”文件描述符表示什么IPC?

Epoll can wait on eventfd . Epoll可以等待eventfd In your case, I'd create an eventfd and than trigger this event. 就您而言,我将创建一个eventfd然后触发该事件。 I think, this is the cleanest solution. 我认为,这是最干净的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM