Execution Concurrency Strategies


The Strategy pattern enables a family of algorithms to be interchangeable. The benefits of using the Strategy pattern are that the algorithms are not hard-coded into the class and that the class can use the appropriate algorithm at different times thus making the class easier to maintain and more flexible. The Execution Concurrency Strategy defines a concurrency policy for executing a runnable object. This strategy varies by the thread context in which the runnable object's void* run() method will execute. Three types of strategies are provided and include the following.

The os_same_thread_executor class implements the same thread execution concurrency strategy. The execute() member function runs the void* run() method of the handler argument in the same thread of control. The os_new_thread_executor class implements the separate thread execution concurrency strategy. The execute() member function runs the void* run() method of the handler argument in a new thread of control. The os_thread_pool_executor class implements the thread pool execution concurrency strategy. It uses the os_thread_pool class for its implementation. The execute() member function enqueues the handler for execution by a thread pool. The void* run() method of the handler is executed by the next available idle thread in the pool. Refer to os_thread_pool class for more information on thread pools.

These classes can be used to execute handlers in the os_tcp_acceptor and os_tcp_connector classes.

Use of member template feature in the execute methods gives the flexibility of not having the handler classes to inherit from a specific runnable interface class.

For examples that illustrates how the os_same_thread_executor , os_new_thread_executor , and os_thread_pool_executor classes would be used, refer to the acceptor1s.cpp, acceptor2s.cpp , and acceptor4s.cpp examples, respectively, in the os_tcp_acceptor , os_tcp_connector section of this chapter.


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.