Our example demonstrates how to implement a worker pool using tasks and protected objects in Ada.
This Ada program implements a worker pool using tasks and protected objects. Here’s a breakdown of the main components:
We define a Job_Queue protected type to safely manage job distribution among workers.
A Result_Queue protected type is used to collect results from workers.
The Worker task type represents our workers. Each worker continuously tries to get a job, process it (simulated by a 1-second delay), and add the result to the result queue.
In the main procedure, we create 3 worker tasks, add 5 jobs to the job queue, and then collect all the results.
To run this program, save it as worker_pools.adb and compile it using an Ada compiler:
The output will show the 5 jobs being executed by various workers. The program takes about 2 seconds to complete despite doing about 5 seconds of total work because there are 3 workers operating concurrently.
This example demonstrates Ada’s built-in support for concurrent programming using tasks and protected objects, which provide a safe and structured way to implement worker pools and other concurrent patterns.