Keywords: Priority

# Priorities

Priority levels can be assigned to individual tasks or task groups. The library supports three levels {low, normal, high} and two kinds of priority:

- Static priority for enqueued tasks.
- Dynamic priority for task groups.

The former is specified by an optional argument of the `task::enqueue()` method, affects a specific task only, and cannot be changed afterwards. Tasks with higher priority are dequeued before tasks with lower priorities. The latter affects all the tasks in a group and can be changed at any time either via the associated `task_group_context` object or via any task belonging to the group. The priority-related methods in `task_group_context` are described in Section task_group_context. The task scheduler tracks the highest priority of ready tasks (both enqueued and spawned), and postpones execution of tasks with lower priority until all higher priority task are executed. By default, all tasks and task groups are created with normal priority.

### Note

Priority changes may not come into effect immediately in all threads. So it is possible that lower priority tasks are still being executed for some time even in the presence of higher priority ones.

When several user threads (masters) concurrently execute parallel algorithms, the pool of worker threads is partitioned between them proportionally to the requested concurrency levels. In the presence of tasks with different priorities, the pool of worker threads is proportionally divided among the masters with the highest priority first. Only after fully satisfying the requests of these higher priority masters, will the remaining threads be provided to the other masters.

Though masters with lower priority tasks may be left without workers, the master threads are never stalled themselves. Task priorities also do not affect and are not affected by OS thread priority settings.

### Note

Worker thread migration from one master thread to another may not happen immediately.

Related Constants and Methods

    namespace tbb {
        enum priority_t {
            priority_normal = implementation-defined,
            priority_low = implementation-defined,
            priority_high = implementation-defined
        };

        class task {
            // . . .
            static void enqueue( task&, priority_t );
            void set_group_priority ( priority_t );
            priority_t group_priority () const;
            // . . .
        };
    }

The following table provides additional information on the members of this template class.
Member | Description
--- | ---
void enqueue ( task& t, priority_t p ) const | Enqueues task `t` at the priority level `p`. ### Note The priority of an enqueued task does not affect priority of the task group, from the scope of which `task::enqueue()` is invoked. That is, the group, which the task returned by `task::self()` method belongs to.
void set_group_priority ( priority_t ) | Changes priority of the task group, which this task belongs to.
priority_t group_priority () const | Returns: Priority of the task group, which this task belongs to.

Parent topic: Task Scheduler

## See Also

task_group_context