我想等待一个文件描述符和互斥,什么是推荐的方法来做到这一点? [英] I want to wait on both a file descriptor and a mutex, what's the recommended way to do this?

查看:118
本文介绍了我想等待一个文件描述符和互斥,什么是推荐的方法来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成线程以执行某些任务,并使用线程安全队列与他们进行通信。我也想在我等待的时候对各种文件描述符进行IO。

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 descriptors while I'm waiting.

这是什么推荐的方法?当队列从没有元素到一些元素时,我必须创建一个线程间的管道并写入它吗?是不是有更好的方法?

What's the recommended way to accomplish this? Do I have to created an inter-thread pipe and write to it when the queue goes from no elements to some elements? Isn't there a better way?

如果我必须创建线程间的管道,为什么没有更多的库实现共享队列允许您创建共享队列和线程间管道作为一个单独的实体?

And if I have to create the inter-thread pipe, why don't more libraries that implement shared queues allow you to create the shared queue and inter-thread pipe as a single entity?

我想这样做是否意味着基本的设计缺陷?

Does the fact I want to do this at all imply a fundamental design flaw?

我问这个关于C ++和Python。我对一个跨平台的解决方案很感兴趣,但主要对Linux感兴趣。

I'm asking this about both C++ and Python. And I'm mildly interested in a cross-platform solution, but primarily interested in Linux.

更具体的例子...

我有一些代码将在文件系统树中搜索东西。我有几个沟通渠道通过套接字向外界开放。可能(或可能不会)导致需要在文件系统树中搜索内容的请求将到达。

I have some code which will be searching for stuff in a filesystem tree. I have several communications channels open to the outside world through sockets. Requests that may (or may not) result in a need to search for stuff in the filesystem tree will be arriving.

我要隔离搜索的代码在一个或多个线程中的文件系统树中的东西。我想采取的请求,导致需要搜索树,并把它们放在一个线程安全的事情,由搜索线程做的队列。结果将被放入已完成搜索的队列中。

I'm going to isolate the code that searches for stuff in the filesystem tree in one or more threads. I would like to take requests that result in a need to search the tree and put them in a thread-safe queue of things to be done by the searcher threads. The results will be put into a queue of completed searches.

我希望能够在搜索正在进行时快速处理所有非搜索请求。我想能够及时地对搜索结果采取行动。

I would like to be able to service all the non-search requests quickly while the searches are going on. I would like to be able to act on the search results in a timely fashion.

服务传入的请求通常意味着某种事件驱动的架构,使用 epoll 。磁盘搜索请求的队列和结果的返回队列将暗示使用互斥体或信号量实现线程安全的线程安全队列。

Servicing the incoming requests would generally imply some kind of event-driven architecture that uses epoll. The queue of disk-search requests and the return queue of results would imply a thread-safe queue that uses mutexes or semaphores to implement the thread safety.

标准方式等待一个空队列是使用条件变量。但是,如果我在等待的时候需要服务其他请求,这将不会工作。或者我最终总是轮询结果队列(并且平均将结果延迟一半的轮询间隔),阻塞和不服务请求。

The standard way to wait on an empty queue is to use a condition variable. But that won't work if I need to service other requests while I'm waiting. Either I end up polling the results queue all the time (and delaying the results by half the poll interval, on average), blocking and not servicing requests.

推荐答案

每当使用事件驱动架构时,需要有一个机制来报告事件完成。在Linux上,如果使用文件,则需要使用select或poll系列中的某些内容,意味着使用管道停止所有无文件相关的事件。

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.

编辑:Linux具有 eventfd timerfd 。这些可以添加到您的 epoll 列表中,并用于在从另一个线程触发或从另一个线程触发时用于突破 epoll_wait 一个计时器事件。

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.

还有一个选项,就是信号。可以使用 fcntl 修改文件描述符,以便在文件描述符变为活动时发出信号。然后,信号处理程序可以将文件就绪消息推送到您选择的任何类型的队列上。这可能是一个简单的信号量或互斥体/ condvar驱动队列。因为现在不再使用 select / poll ,不再需要使用管道来排队基于文件消息。

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.

健康警告:我没有尝试过,虽然我不明白为什么它不工作,我真的不知道的性能影响 signal 方法。

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.

这篇关于我想等待一个文件描述符和互斥,什么是推荐的方法来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆