Preliminary. ThinkMeta ConcurrentTasks and this documentation are preliminary; everything is subject to change.

Tasks, fibers and threads

On this page

A task, a fiber and a thread are three different things in ConcurrentTasks. A task is not a fiber: it has one only while it is under way, from the call of its Run until its exit.

What it is Whose it is How many there are
Task An object with a Run method: one piece of work. Yours. As many as you create.
Fiber An execution context with a stack of its own, on which Run is called. The core and the debugger call it a task context. The scheduler's, which keeps its fibers in a pool. About as many as tasks were under way at the same time.
Thread An OS thread of the scheduler. It runs one fiber at a time. The scheduler's. numberOfThreads, by default one per logical processor.

From AddTask to exit

What happens The task Its fiber The thread
AddTask is queued; the queue holds a pointer to it none yet —
The task starts Run is called the fiber whose task has just exited, or one from the pool; a new one only if the pool is empty the one that took the task from the queue
The task waits is suspended stays with the task, its stack as it was switches to another fiber and runs other tasks, or sleeps
The task is resumed is queued again, now with its fiber the same any thread of the scheduler continues it
Run returns has exited once OnTaskExited has been called; it is yours again takes the next new task, or goes back to the pool stays on the fiber, switches to another one, or sleeps

A task that yields and gives up its thread is treated like a resumed task: it keeps its fiber and is queued again. YieldFlags::HigherPriority returns at once, without a switch, if no High work is waiting.

The last row is what makes short tasks cheap. A fiber whose task has exited does not end with it. If a new task is next in the queues, the fiber calls that task's Run right away: no fiber switch, no other stack. Only when a resumed task comes first, or when there is nothing to do, does the fiber go back to the pool. A task that never waits costs a queue operation and a call.

Nine tasks on three fibers and two threads
Two threads, three fibers, nine tasks. Tasks follow each other on a fiber without a switch. T3 waits and keeps fiber A, so thread 1 goes on with another fiber. Once T5 has exited and T3 has been resumed, thread 2 puts fiber B back into the pool and continues T3 on fiber A. A sketch, not a measurement.

How many fibers there are

  • A queued task has no fiber. It takes one slot of a task queue and nothing else, so the number of queued tasks is bounded by taskQueueCapacity, not by stacks.
  • The pool grows to the peak of tasks under way, plus at most one fiber per thread. Tasks that never wait or yield need at most one fiber per thread, however many they are. Every task that waits or has yielded holds one more fiber until it runs again.
  • Resumed tasks come before new ones of the same priority (Queues and their capacity). A task that already has a fiber goes on before a new task takes another one.
  • The pool does not shrink. Fibers and the stack memory they have used stay with the scheduler until it is destroyed. A short peak of waiting tasks keeps its stacks.
  • There is an upper limit: 1,048,576 fibers in a default scheduler, 65,536 in a local or a dispatcher scheduler. Exceeding it is invalid use and ends the process.

Stacks

Every fiber of a scheduler has a stack of the same size, taskStackSize. The size is address space that is reserved; memory is committed only for the part a task uses. Zero means the default: on Windows the stack reserve in the header of the executable (1 MiB unless the linker was told otherwise), on Linux 1 MiB.

A stack does not grow beyond its size, so it must hold the deepest call chain of any task. Below each stack lies a guard zone: an overflow faults instead of overwriting memory.

What belongs to the task, the fiber and the thread

State Belongs to Across a wait or a yield
The members of the task object the task unchanged
Local variables and the call chain below Run the fiber unchanged and at the same addresses: another task may use a pointer to a local variable while this task waits
thread_local values, the thread id, OS locks, a COM single-threaded apartment the thread may be another thread's afterwards; do not carry them across

A TaskHandle, as GetCurrentTask returns it, names a task under way together with its fiber. It is valid until the task exits; after that the same value may name another task.

The other schedulers

The sections above describe the default scheduler. The others combine the three differently:

Threads Fibers
Local scheduler the thread that calls Run as above; the tasks never change threads
Dispatcher scheduler the thread it is bound to from a pool as well, with the default stack size; a task gets its fiber when it is added or run, and the fiber always goes back to the pool when the task exits
TaskSwap one one for each of its two tasks
Converted thread the thread itself none, only a task context without a stack of its own: the thread's stack is the task's, and waiting blocks the thread

Compared with other schedulers

Schedulers that run many units of work on few threads differ in when a unit of work gets a stack. Where the unit of work is itself the fiber or green thread, it has one from its creation: Go gives a goroutine its stack at the go statement and keeps it for reuse when the goroutine has ended. ConcurrentTasks keeps the unit of work and the execution context apart, as job systems on fibers do: Google's marl, for example, also runs queued tasks one after another on a fiber and continues unblocked fibers before it starts new tasks. See Why fibers for the wider comparison.

See also