Boss and Workers Pattern
Overview of Boss-Worker Pattern
The boss-worker pattern is a multi-threading model characterized by a single boss thread and multiple worker threads.
The boss is responsible for assigning tasks to the worker threads.
Each worker is tasked with completing the work assigned to them.
Roles and Responsibilities in the Toy Shop Example
The boss thread’s job includes:
Accepting an order from a customer.
Immediately passing the order to one of the worker threads.
Worker threads are responsible for steps 2 through 6, which include:
Parsing the order
Cutting the pieces
Painting the pieces
Assembling the wooden toy
Shipping the order
Throughput Considerations
The overall throughput of the system is determined by the efficiency of the boss thread.
Specifically, the throughput is inversely proportional to the time the boss spends on each order.
To optimize system performance, it is essential to keep the boss efficient.
Operation Dynamics
The boss thread does not inspect the orders; it simply passes each directly to the workers:
Each worker starts at step two, which minimizes processing done by the boss.
Methods of Passing Work
Direct Signaling to Workers
The boss keeps track of which workers are free and hands off work to them directly.
This method involves signaling a specific worker to accept the order.
Requires the boss to manage availability of workers and await acknowledgment from a specific worker.
Advantages:
No need for workers to synchronize with each other.
Each worker just follows the instructions from the boss.
Disadvantages:
Increased workload on the boss due to tracking worker availability.
Decreased overall throughput as a result of boss’s increased duties.
Using a Shared Queue (Producer-Consumer Model)
The boss acts as the sole producer of work requests (toy orders); workers are the consumers.
The boss accepts an order, places it on a shared queue, and can return to accepting new orders without waiting for worker acknowledgment.
Workers check the queue when free and pick work requests from the front.
Advantages:
Reduces the boss's awareness of worker states
d. - Elimination of need for synchronization during work handoffs.Enhances throughput as the boss can efficiently manage multiple orders.
Disadvantages:
Workers must synchronize their access to the shared queue, leading to potential contention.
Additional synchronization required between workers and boss when checking queue pointers (e.g., to determine if the queue is full or empty).
Conclusion
The shared queue model ultimately reduces the time per order that the boss spends, resulting in better overall system throughput.
This reason underpins the prevalence of the boss-worker pattern in multi-thread applications, due to its efficiency in managing work distribution among threads while balancing synchronization overheads.