It’s fairly easy to start a thread, especially if you simplify the CreateThread function or
encapsulate even more with the launch_thread class. But what do you do with a thread once it’s
started? If you want to send it commands, you have a generic server situation. This class
does all the work to make a simple server.
APC_server. The actual thread is created
by the constructor.
send member.
quit (which will return immediatly), or simply
by destructing the APC_server object (the destructor blocks until the server thread completes shutdown).
Dynamically allocate (i.e. use new) an object derived from abstract_command_object.
Pass this pointer to the send function. The command object is queued and its virtual operator() will
be called eventually. Then the server deletes the command object.
The command queue is inherantly asynchronous. If you want to get results back or wait for the command to complete, you have to provide for that as part of the command object.
Since the command object is deleted by the server, you can’t have it hold result data. Instead, include a pointer to the result area in the command object. You can also include an event_flag in the command object in order to wait for completion.