Parallel Worker
This crate provides a simple interface for running tasks in parallel.
The BasicWorker
, CancelableWorker
or OrderedWorker
structs are used to dispatch tasks to worker threads and collect the results. You can wait for results or recieve currently available results.
Workers
There are three types of workers:
BasicWorker
is a simple worker that processes tasks in parallel using multiple worker threads.CancelableWorker
has additional functionality for optional results and task cancelation during execution.OrderedWorker
returns results in the same order as the tasks were added.
Example
Basic example of using a worker to run tasks in parallel using the BasicWorker
struct.
Tasks start executing as soon as they are added. When all threads are busy, tasks are queued until a thread becomes available.
use *;
Tasks can be canceled
If you want to cancel tasks during execution, use CancelableWorker
and call the check_if_cancelled!
macro in your worker function on a regular basis. Exessive checking will lead to a performance costs.
Canceled tasks will stop executing as soon as they reach a check_if_cancelled!
, their results will be discarded.
Results of tasks that have already completed will remain unaffected.
use *;
Results can be optional
If a worker returns [None
] the result will be discarded. This feature is available in the [CancelableWorker
].
use *;
Results can be ordered
If you want to get results in the same order as the tasks were added, use OrderedWorker
.
use *;