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

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

#include <critical.hpp>

Inheritance diagram for tf::CriticalSection:
Collaboration diagram for tf::CriticalSection:

Public Member Functions

 CriticalSection (size_t max_workers=1)
 constructs a critical region of a limited number of workers
 
template<typename... Tasks>
void add (Tasks...tasks)
 adds a task into the critical region
 
- Public Member Functions inherited from tf::Semaphore
 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)
 

Detailed Description

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

tf::CriticalSection is a warpper 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();
class to create a critical region of limited workers to run tasks
Definition critical.hpp:49
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 task handle over a node in a taskflow graph
Definition task.hpp:187
class to create a taskflow object
Definition core/taskflow.hpp:73
T endl(T... args)

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