Correct organization of event processing in a separate thread

Multithreaded application became something common. The main reason for that is execution of resource-intensive tasks in parallel to GUI without stopping it for long operations. As it is well known, the easiest way for asynchronous data processing used by ThreadPool. This method is not only the easiest, but also very efficient. Notwithstanding its merits, it has one large backdraw – an application cannot manage ThreadPool lifetime and cannot be confident that during termination ThreadPool doesn't process previously assigned tasks that may cause application crash.

Let's review a specific case of threadpool implementation consisting of a single thread. The requirements to this implementation are:

Delay between adding task to threadPool and initiation of its execution should be minimal

If there are no tasks, threadpool should not consume resources. Specifically, context switches should not be allocated to thread that doesn't process tasks

Threadpool should provide an interface (IDisposable is the best for it) for correct work termination and guarantee that upon Dispose() call, all tasks including tasks in execution shall be completely terminated.

Tasks should be executed in sequence without acquiring synchronizing objects.

Enable recursive task execution

Providing interface for exception handling.

No comments:

Post a Comment