tf::CriticalSection class | Taskflow QuickStart

# tf::CriticalSection class

class to create a critical region of limited workers to run tasks

tf::CriticalSection is a wrapper over tf::Semaphore and is specialized for limiting the maximum concurrency over a set of tasks. A critical section starts with an initial count representing that limit. When a task is added to the critical section, the task acquires and releases the semaphore internal to the critical section. This design avoids explicit call of tf::Task::acquire and tf::Task::release. The following example creates a critical section of one worker and adds the five tasks to the critical section.

    tf::Executor executor(8);   // create an executor of 8 workers
    tf::Taskflow taskflow;

    // create a critical section of 1 worker
    tf::CriticalSection critical_section(1);

    tf::Task A = taskflow.emplace([](){ std::cout << "A" << std::endl; });
    tf::Task B = taskflow.emplace([](){ std::cout << "B" << std::endl; });
    tf::Task C = taskflow.emplace([](){ std::cout << "C" << std::endl; });
    tf::Task D = taskflow.emplace([](){ std::cout << "D" << std::endl; });
    tf::Task E = taskflow.emplace([](){ std::cout << "E" << std::endl; });

    critical_section.add(A, B, C, D, E);

    executor.run(taskflow).wait();

## Base classes

class Semaphore
  class to create a semophore object for building a concurrency constraint

## Constructors, destructors, conversion operators

CriticalSection(size_t max_workers = 1) explicit
  constructs a critical region of a limited number of workers

## Public functions

template<typename... Tasks>
void add(Tasks... tasks)
  adds a task into the critical region

Tab / T to search, Esc to close
…

Search for symbols, directories, files, pages or modules. You can omit any prefix from the symbol or file path; adding a `:` or `/` suffix lists all members of given symbol or directory.

Use ↓ / ↑ to navigate through the list, Enter to go. Tab autocompletes common prefix, you can copy a link to the result using ⌘ L while ⌘ M produces a Markdown link.

Sorry, nothing was found.