Handling asynchronous callbacks invocation
One simple situation we may encounter in multithreaded programming is when we need to run a method on another thread. For example, when a download task completes on a worker thread, the main thread may want to be notified of the task completion, to parse the downloaded data. In this recipe we will implement a mechanism for such notifications.
Getting ready
Understanding of the asynchronous event concept is important before we proceed to the implementation details. When we say asynchronous, we mean that something occurs unpredictably and has no determined timing. For example, we cannot predict how long it will take our task to download a URL—that is it; the task completes asynchronously and should invoke a callback asynchronously.
How to do it…
The message for us should be a method call. We will hide a method call behind this interface:
class iAsyncCapsule: public iObject { public: virtual void Invoke() = 0; };
A pointer to an instance of such type represents...