I have an implementation of a bi-directional message channel, which, in order to reduce overhead, I implemented as a pair of circular message buffers. To write from one end to the other, you add a pointer to the message in one circular buffer and configure read and write indexes for it. To write in a different direction, you do the same for another buffer, and so on. The code is small and simple, and it avoids the overhead of using a pipe or fifo, although perhaps this might be the best solution in some way.
I implemented the survey by simply checking to see if there is a message waiting to be read, and if the expected timeout of the condition variable that receives the signal when the message is added to the corresponding array is not fulfilled.
Now I have an application that needs to wait on the socket (more or less) and on the message channel at the same time. I wish I had used a filo or pipe, but due to the overhead of changing the code (long story) it would be impossible to rewrite it to use fifo or pipe.
Is there a way to get the file descriptor associated with a condition variable? If so, it is easier to poll on two file descriptors at once, one for the condition variable and one for the socket.
Out of curiosity and making this question more useful for others with a similar problem, is it possible to get a file descriptor associated with a semaphore so that you can poll the semaphore and the regular file descriptor at the same time?
source
share