Taskflow  3.2.0-Master-Branch
Loading...
Searching...
No Matches
tf::Semaphore Class Reference

class to create a semophore object for building a concurrency constraint More...

#include <semaphore.hpp>

Inheritance diagram for tf::Semaphore:

Public Member Functions

 Semaphore (size_t max_workers)
 constructs a semaphore with the given counter
 
size_t count () const
 queries the counter value (not thread-safe during the run)
 

Friends

class Node
 

Detailed Description

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
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();
class to create an executor for running a taskflow graph
Definition executor.hpp:50
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:742
class to create a semophore object for building a concurrency constraint
Definition semaphore.hpp:68
Task & acquire(Semaphore &semaphore)
makes the task acquire this semaphore
Definition task.hpp:470
Task & release(Semaphore &semaphore)
makes the task release this semaphore
Definition task.hpp:479
class to create a taskflow object
Definition core/taskflow.hpp:73
T endl(T... args)

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.

Constructor & Destructor Documentation

◆ Semaphore()

tf::Semaphore::Semaphore ( size_t  max_workers)
inlineexplicit

constructs a semaphore with the given 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

The documentation for this class was generated from the following file: