Executors
-
A protocol that defines an object that can synchronously drive futures to completion and can be waited on until it’s empty.
Blocking executors must explicitly be asked to run, using the
See morerun()
method, to actually perform work.Declaration
Swift
public protocol BlockingExecutor : ExecutorProtocol
-
A thread-safe unbounded executor backed by a serial Dispatch queue.
Submitting futures into this executor from any thread is a safe operation.
Dropping the last reference to the executor, causes it to be deallocated. Any pending tasks tracked by the executor at the time are destroyed as well.
See moreDeclaration
Swift
public final class QueueExecutor : ExecutorProtocol, Cancellable
-
Declaration
Swift
public final class RunLoopExecutor : ExecutorProtocol
-
An executor that polls futures on the current thread.
ThreadExecutor
is typically used to execute futures synchronously. Usewait()
to run the executor until all submitted futures complete orrunUntil(_:)
to run until a given future completes. Both methods block the current thread when no more progress can be made.ThreadExecutor
can also be used to integrate Futures with other asynchronous systems. For example, each worker thread in a thread pool could maintain a privateThreadExecutor
instance and run it on every tick usingrun()
. This method does not block the current thread. Instead, the executor runs until all possible progress is made and then returns.ThreadExecutor
can optionally be initialized with limited capacity; that is, it may be configured to have an upper bound on the number of futures it tracks. Submitting a future that would result in the executor exceeding capacity throws an error. You are encouraged to choose an appropriate capacity for each use case, in order for the executor to provide backpressure and limit memory usage or reduce latency. The executor efficiently tracks its futures regardless of configured capacity; that is, during each run it only polls futures that have signalled that they can make progress.Each thread automatically gets an unbounded
ThreadExecutor
instance that is accessed via thecurrent
static property. Instances are lazily created on first access and are stored in a thread local viapthreads
. Note that your code must still ensure it regularly callsrun()
or one of thewait
methods to run the executor.
See moreThreadExecutor
is safe to use from one thread only, which is typically the thread that created the instance. Submitting futures into the executor or running the executor from multiple threads concurrently is undefined behavior and will most likely result in a crash. The executor, however, can handle concurrent wakeups from any number of threads.Declaration
Swift
public final class ThreadExecutor : BlockingExecutor