I want to wait on both a file descriptor and a mutex, what's the recommended way to do this?

后端 未结 8 1261
-上瘾入骨i
-上瘾入骨i 2020-12-31 03:36

I would like to spawn off threads to perform certain tasks, and use a thread-safe queue to communicate with them. I would also like to be doing IO to a variety of file descr

8条回答
  •  执念已碎
    2020-12-31 03:45

    Whenever one uses an event driven architecture, one is required to have a single mechanism to report event completion. On Linux, if one is using files, one is required to use something from the select or poll family meaning that one is stuck with using a pipe to initiate all none file related events.

    Edit: Linux has eventfd and timerfd. These can be added to your epoll list and used to break out of the epoll_wait when either triggered from another thread or on a timer event respectively.

    There is another option and that is signals. One can use fcntl modify the file descriptor such that a signal is emitted when the file descriptor becomes active. The signal handler may then push a file-ready message onto any type of queue of your choosing. This may be a simple semaphore or mutex/condvar driven queue. Since one is now no longer using select/poll, one no longer needs to use a pipe to queue none file based messages.

    Health warning: I have not tried this and although I cannot see why it will not work, I don't really know the performance implications of the signal approach.

    Edit: Manipulating a mutex in a signal handler is probably a very bad idea.

提交回复
热议问题