ThreadExecutor

public final class ThreadExecutor : BlockingExecutor

An executor that polls futures on the current thread.

ThreadExecutor is typically used to execute futures synchronously. Use wait() to run the executor until all submitted futures complete or runUntil(_:) 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 private ThreadExecutor instance and run it on every tick using run(). 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 the current static property. Instances are lazily created on first access and are stored in a thread local via pthreads. Note that your code must still ensure it regularly calls run() or one of the wait methods to run the executor.

ThreadExecutor 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.