class
#include <src/taskflow/core/semaphore.hpp>
Semaphore class to create a semophore object for building a concurrency constraint
Contents
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::
tf::Executor executor(8); // create an executor of 8 workers tf::Taskflow taskflow; tf::Semaphore semaphore(1); // create a semaphore with initial count 1 std::vector<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 organization limits the number of concurrently running tasks to only one.
Derived classes
- class CriticalSection
- class to create a critical region of limited workers to run tasks
Constructors, destructors, conversion operators
- Semaphore(int max_workers) explicit
- constructs a semaphore with the given counter
Public functions
- auto count() const -> int
- queries the counter value (not thread-safe during the run)