C++ threading could be so simple...
I will appreciate some feedback on these thoughts:
Threads with undeterministic communication (semi-random input/output)
are usually (and best) implemented with some kind of message loop.
However, this gives the headache of having to maintain a switch that,
based on a message ID, cast's the generic base class to a concrete
message. It also means that you have to define the messages and give
them members to convey the needed information.
This is usually quite trivial: To keep the switch as small as
possibly, you would probably use the message ID to cast the message,
extract the members and call a function that handles that specific
message (passing it the members of the message).
This means that you could might as well send the receiving thread a
functor. The receiving thread should simply expose an interface (e.g.
a few functions - possibly members of an abstract class). The calling
thread could then send a functor (in a generic message with a 'virtual
void dispatch()' function) that have the address of the function (and
its arguments) to be executed in the context of the receiving thread:
int foo(int); // Event to trigger in thread B
class MessageBase { public: virtual void dispatch() = 0; };
template</* ... */>
class Message : public MessageBase { /* ... */ };
template</* ... */>
MessageBase* make_message(/* ... */);
void thread_a() {
// ...
thread_b_queue.push_back(make_message(&foo, 42));
// ...
}
void thread_b() {
while (true) {
MessageBase* msg = thread_b_queue.get(); // Blocking call
msg->dispatch(); // Thread 'b' executes the call to 'foo(42)'
delete msg;
}
}
Using futures/promises, it will even be possible for thread_a() to get
the value returned from the call to 'foo()'. The promise shall simply
be passed to the Message object.
You can find a working example (sorry but there is not a Makefile)
here: http://cvs.savannah.gnu.org/viewvc/gptm/examples/example01/?root=gptm
The 'library' is stored as a project on Savannah: https://savannah.nongnu.org/projects/gptm/
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]