我们可以获取信号量或条件变量文件描述符? [英] Can we obtain a file descriptor for a semaphore or condition variable?

查看:275
本文介绍了我们可以获取信号量或条件变量文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,为了减少开销,我实施为一对消息的循环缓冲区的一个双向消息信道的实现。向从一端写入其他你的指针添加到消息到一个圆形缓冲器和调整的读取和写入指数它。以在另一个方向上写这样做的另一个缓冲器等相同。在code是小而简单,避免使用管道或FIFO的开销,虽然可能可能已经在某些方面更好的解决方案。

我通过简单的检查,如果有消息等待读的,如果不这样做就当消息被添加到相关的阵列被标志着一个条件变量计时的等待实施了这个调查。

现在我有需要(或多或少)和该消息的信道的同时一个插座上等待的应用程序。现在,我想我已经在得到code使用FIFO或管道,但由于开销改变(长的故事),它不是真的可行重写它使用FIFO或管道。

有没有什么办法让一个条件变量相关的文件描述符?如果是,则是更容易实现的一次两个文件描述符,一个用于条件变量和一个用于插座的轮询

出于好奇,并使这个问题给其他人用类似的问题更为有用,是有可能得到与信号相关的文件描述符,所以你可以在信号量,并在同一时间有规律文件描述符轮询<? / p>

解决方案

一般情况下,没有。但不同的操作系统提供你有这个问题的不同解决方案。

窗口

您可以用 WSAEventSelect WaitForMultipleObjectsEx 等待套接字或互斥或数据信号灯事件等。

的Linux

您可以使用带有FUTEX_FD论点 futex的系统调用(然而,这已经从内核中移除),或使用 eventfd 实施条件变量。

和你可以产卵,将在条件变量上等待第二个线程,和信号之一选择等待()。或信号问什么时候套接字上接收输入等请参见此相关的问题

I have an implementation of a bi-directional message channel which, to reduce overhead, I implemented as a couple of circular buffers of messages. To write from one end to the other you add the pointer to the message to one circular buffer and tweak the read and write indices for it. To write in the other direction you do the same for the other buffer and so on. The code is small and simple and it avoids the overheads of using a pipe or fifo, although possibly that might have been a better solution in some ways.

I implemented a poll on this by simply checking if there was a message waiting to be read, and if not doing a timed wait on a condition variable that gets signalled when a message is added to the relevant array.

Now I have an application that needs to wait on a socket (more or less) and on the message channel at the same time. I now wish I had used a fifo or pipe, but due to overhead in getting the code changed (long story), it's not really feasible to rewrite it to use a fifo or pipe.

Is there any way to get a file descriptor associated with a conditional variable? If so it is easier to implement a poll on two file descriptors at once, one for the conditional variable and one for the socket.

Out of curiosity, and making this question more useful to others with a similar problem, is it possible to get a file descriptor associated with a semaphore so you could poll on the semaphore and a regular file descriptor at the same time?

解决方案

Generally, no. But different OSes offer different solutions for the problem you have.

Windows

You can associate an event with a socket with WSAEventSelect and wait with WaitForMultipleObjectsEx for the data on the socket or mutex or semaphore event etc.

Linux

You can use the futex syscall with FUTEX_FD argument (however this has been removed from the kernel), or use eventfd to implement the condition variable.

And you can spawn a second thread that would be waiting on the condition variable, and signal the one waiting in select(). Or ask for signals when input is received on the socket, etc. See this related question.

这篇关于我们可以获取信号量或条件变量文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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