This is an issue, that I have come acrosss earlier. Basically a c++ object has a member object that does some work, once the work is done , a notification needs to made to the parent. What is the most elegant solution to allow this communication. Does being in this position indicate a flaw with the design to begin with?
To elaborate.
class A {
B member;
void do_something();
}
class B{
void talk_to_network();
};
void 开发者_如何学JAVAdo_something()
{
//Conditional wait on a variable that will change when talk to network completes.
//So need a way for B to inform A, that it is done.
}
Unless you are talking about concurrency (threads), the usual approach to return a result from an operation is to return a value from a function.
It probably depends on what you mean by "member object". Do you mean a data member of some class?
class A { };
class B
{
A member;
};
If A
has a member function that "does some work", that member function should (first approximation) return the result as its normal return value. That's the simplest way to do this kind of thing. So B
calls the method and is returned the result.
If it has to return multiple results, you can pass a function-like object to A
's member function, so it can call back to it.
Since you're apparently dealing with multithreading, the normal way would be to pass the address of some sort of signaling variable from A to B when do_something
calls talk_to_network
. The exact type used for signaling varies with the OS -- for Windows, you'd probably use an Event
. If you're using pthreads, then you'd probably use a condition variable
for the signaling.
The best way to implement would probably be with the Observer pattern. It's an event like pattern.
You want to use a condition variable. You can either poll (timed wait) or block waiting for the worker thread to finish.
If you using boost: http://www.boost.org/doc/libs/1_42_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref
When using concurrency, there are generraly two generally algorithms: pending and signaling.
One thread can wait, or pend, on an event flag. The processing thread would set the event after processing is completed. One thorn to this technique is that the thread that pends is stopped until the event is set.
On signaling, the control thread sends the request to the processing task, then continues performing other tasks. The processing task will send a signal to the control thread, interrupting it, when processing is finished. Instead of interrupting, the controlling thread may be able to occasionally poll an event flag.
In either case, signal and event processing are an operating system issue and differ depending on the operating system. C and C++ languages have no standard facilities for signal and event processing.
In our office we use a slightly modified implementation of Boost Signals for this purpose. We've had good success with it. In your example, A
registers a callback function with a signal. When B
is finished with its work, it calls the signal to let A
know it is done.
精彩评论