Hey everyone,
I'm writing a threaded database server application and need to communicate the results of several worker threads to a single multiplexed communication output thread. Basically, the communication output thread sits in a select statement waiting for its client sockets to dole out requests, connect, disconnect, and become ready for sending data. This thread is, therefore, using a select statement, and a round robin scheduling policy, to control its scheduling.
Now I have several other threads whos sole purpose is to take the longer, possibly blocking, tasks and construct a deliverable for the client. Of course, when they are through they need to send that data to the communication output thread who is sitting in the select waiting for socket events.
Well, unless I'm missing something there is no select call that will allow me to use typical thread synchronization with mutexes and condition variables and mix this with my other file descriptors. So I'm guessing my only option is to be able to interrupt the select. This means that either I need to signal the thread to break out of the select. This seems unreliable in my opinion given that the thread that handles said signal is randomly selected from a set of threads not having that signal blocked and therefore there exists a small window where a new thread is being created in which it can obtain a signal it is just about to tell the scheduler it wants to block.
Therefore, it seems I have no choice but to use a file descriptor and set its read, write, or execute condition such that when there is information waiting the select will be capable of seeing it and when there is no more data waiting I can remove said condition. This all, of course, needs to be mutex protected, but only when I bounce out of the thread do I need to attempt to attain said mutex.
Basically, I decided to use a pipe and following this procedure:
select for readability on read fd of pipe, when readable:
lock mutex
pull data off mutex protected in memory queue
if end of queue read char off pipe
unlock mutex
handle data
Then to signal this:
lock mutex
push data until mutex protected in memory queue
if first item put char on pipe
unlock mutex
That should work...but it seems wasteful to use up two file descriptors and an OS pipe buffer for a single character which is nothing more than an on/off switch. What I'm wondering is, for those who have made it this far, is there anything more lightweight I can use that could acheive the same behavior, or is how I'm going about this completely rediculous?
I'm looking for ideas...so any would be appreciated...if not it looks like I'm going to use the above method...I just wonder if there is something more standard that I could do or something better.
Thanks everyone,
Brian Gregg
Computer Sciences Corp.