Thread pooling describes a technique by which threads of execution are managed and to which work is distributed. Additional semantics such as concurrency control may also be defined. Thread pooling is a nice way to perform the following tasks:
Thread pooling is a natural fit for state-based processing. if you can decompose your system into a set of state machines, thread pooling works nicely and effectively in realizing your design. This provides the added benefit of simplifying the debugging of multithreaded applications in most cases.
Make your applications scalable
Properly implemented, the thread pool can enforce concurrency limits that will make your application scalable.
Introduce new code while minimizing risk
Thread pooling lets you break execution into work units that are best described as development sandboxes. Sandboxes are fun and safe! How? Thread pooling promotes loose coupling between processes and naturally separates data from process. Any coupling between processes typically happens at a well-defined data point. This can be a lot easier to maintain over time, especially in large multithreaded applications.
The conceptual model of a thread pool is simple: The pool starts threads running; work is queued to the pool; available threads execute the queued work. By using templates, the pool may be defined independent of the thread/work implementation (a technique known as static polymorphism).
Figure 1. Thread pool collaboration diagram
The thread pool is responsible for thread creation; threads commence execution at worker::thread_proc. Requests are queued to the thread pool; the worker prepares the request and the request is queued. When a thread is available to process the work, it may request the pending work from the thread pool with thread_pool::get_queued_status. If there is no pending work, the thread is suspended until work is available.
While our worker implementation allows us to queue work, we can go one step further. The thread pool promises to help us break problems into discrete steps that maintain state minimizing complexity and risk while maximizing the raw power we can squeeze out of our box. However, our current implementation only allows us to queue one piece of work at a time, making it cumbersome to logically group sequential work together. We also need some way of knowing when that work is done so that we might queue some more work.
Take the system down; rebuild the system data; bring the system back online.
Three steps, three pieces of work. I would like to be able to use this instead:
(core::chain(), new system_down, new rebuild_data,
What is chain? A work unit! Rather, our chain simply acts as a container for the real work unit, which in turn is just a container of work units:
// if not empty, requeue
if (true == empty()) return;
Using the Code
Initialize the thread pool you want to use. Because thread pools are parameterized singletons, there will be a thread pool instance for each type of worker used. The global::thread_pool class is a convenient typedef for core::thread_pool<core::worker_thread>.
If you choose core::worker_thread as your worker implementation, all work will be derived from core::work_unit and your work will be performed when the process is called.
struct mywork : core::work_unit
void process() throw()
// work is processed here
To queue work, create an instance of your class and initialize it as necessary. Use thread_pool::queue_request to queue the work.
// demonstrate chaining
(core::chain(), new work_1, new work_2, new work_3));
To shut down the thread pool, use thread_pool::shutdown.
To create other thread pools, simply define a worker thread and its constituent parts. The worker thread must define request_type, prepare_request, and thread_proc. The LPVOID parameter passed to thread_proc is always 0, and should not be used. The thread pool may be extended to provide context through this parameter. The code below defines an io::thread and an io::thread_pool.
Users may access io::thread_pool with io::thread_pool::instance();.
About the Demo Program
The demo program does the following:
Initializes the global::thread_pool instance.
Instantiates three different types of work.
Instantiates a chain tying the work together.
Queues and processes the work.
Shuts down the thread pool.
If you are a member of a team, you can quickly divide and distribute the work to the team to implement as work units. Each work unit can be tested independently and integrated as a final product. Each person has a sandbox to play in.
Thread pools are a fantastic tool for writing large, scalable systems quickly and safely without sacrificing performance.
About the Author
Joshua Emele lives in San Francisco. A member of Plugware Solutions, Ltd. he specializes in network, database, and workflow applications in c++. He is madly in love with life and his partner and enjoys playing classical guitar, hiking, and digital electronics.
Plugware Solutions, Ltd. provides design, review, integration and implementation consulting services and is the maker of the Plugware Web Services Platform.
Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …
Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …