Threaded vs. event-driven C++ portable app

  • From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 7 Apr 2010 16:45:49 +0200

Hi all,

I'm facing a problem which is probably not so difficult to solve but I
really don't know how to do the best possible.

The application has a thread which reads as fast as possible from a TCP
socket. These received data are requests coming from outside the app and
which must be treated as soon as possible but they are not so frequent.
In other words, there can be a great amount of time during which the
thread which treats requests has nothing to do at all. At this point,
the thread could have two kinds of events to handle: Either new data
arrived on the socket and must be treated asap, or the thread end is
requested.

The simplest way I implemented this was to perform a timed "select()" on
the input socket. When the "select()" call returns, I first check for
thread end requested and then, if thread is not requested to end, I
treat the data and loop back to the timed "select()". Although this
simple approach is interesting for fast request treatment, we may wait
at most the "select()" timeout period before application quits if we
request thread end while no data is incoming.

The second solution I thought about was to introduce a file descriptor
in addition to the input socket and to perform the "select()" on both
file descriptors. This way, thread end could be treated as soon as new
incoming data but this raises a linux->windows portability problem.

Under linux, an application can perform a "select()" on any type of file
descriptors: sockets, pipes, regular files, ... But under windows, only
socket descriptors are eligible for a "select()".

So here is my question:

Is there a more elegant portable way for a thread to be synchronized
with several sources, (internal and external), than treating all the
synchronization events as external, letting the thread know of internal
events through a socket on the loopback interface?

Many thanks in advance for any advice. Have a nice day. Chris D
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: