# tf::Semaphore class
#include <taskflow/core/semaphore.hpp>

class to create a semophore object for building a concurrency constraint

A semaphore creates a constraint that limits the maximum concurrency, i.e., the number of workers, in a set of tasks. You can let a task acquire/release one or multiple semaphores before/after executing its work. A task can acquire and release a semaphore, or just acquire or just release it. A tf::Semaphore object starts with an initial count. As long as that count is above 0, tasks can acquire the semaphore and do their work. If the count is 0 or less, a task trying to acquire the semaphore will not run but goes to a waiting list of that semaphore. When the semaphore is released by another task, it reschedules all tasks on that waiting list.

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

    tf::Semaphore semaphore(1); // create a semaphore with initial count 1

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

    for(auto & task : tasks) {  // each task acquires and release the semaphore
      task.acquire(semaphore);
      task.release(semaphore);
    }

    executor.run(taskflow).wait();

The above example creates five tasks with no dependencies between them. Under normal circumstances, the five tasks would be executed concurrently. However, this example has a semaphore with initial count 1, and all tasks need to acquire that semaphore before running and release that semaphore after they are done. This arrangement limits the number of concurrently running tasks to only one.

## Constructors, destructors, conversion operators

Semaphore() defaulted
  constructs a default semaphore
Semaphore(size_t max_value) explicit
  constructs a semaphore with the given value (i.e., counter)

## Public functions

auto value() const -> size_t
  queries the current counter value
auto max_value() const -> size_t
  queries the maximum allowable value of this semaphore
void reset()
  resets the semaphores to a clean state
void reset(size_t new_max_value)
  resets the semaphores to a clean state with the given new maximum value

## Function documentation

### tf::Semaphore::Semaphore() defaulted

constructs a default semaphore

A default semaphore has the value of zero. Users can call tf::Semaphore::reset to reassign a new value to the semaphore.

### tf::Semaphore::Semaphore(size_t max_value) explicit

constructs a semaphore with the given value (i.e., counter)

A semaphore creates a constraint that limits the maximum concurrency, i.e., the number of workers, in a set of tasks.

    tf::Semaphore semaphore(4);  // concurrency constraint of 4 workers

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.