tf::Semaphore
taskflow/core/semaphore.hpp
class friend class Node Node tf::Semaphore::Node Node
class friend class Executor Executor tf::Semaphore::Executor Executor
std::mutex std::mutex tf::Semaphore::_mtx _mtx tf::Semaphore::_mtx
size_t size_t tf::Semaphore::_max_value _max_value tf::Semaphore::_max_value {0}
size_t size_t tf::Semaphore::_cur_value _cur_value tf::Semaphore::_cur_value {0}
SmallVector< Node * > SmallVector<Node*> tf::Semaphore::_waiters _waiters tf::Semaphore::_waiters
tf::Semaphore::Semaphore
()=default
Semaphore
tf::Semaphore::Semaphore

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)
Semaphore
tf::Semaphore::Semaphore
size_t max_value

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::Semaphoresemaphore(4);//concurrencyconstraintof4workers

size_t size_t tf::Semaphore::value () const value tf::Semaphore::value

queries the current counter value

size_t size_t tf::Semaphore::max_value () const max_value tf::Semaphore::max_value

queries the maximum allowable value of this semaphore

void void tf::Semaphore::reset () reset tf::Semaphore::reset

resets the semaphores to a clean state

void void tf::Semaphore::reset (size_t new_max_value) reset tf::Semaphore::reset size_t new_max_value

resets the semaphores to a clean state with the given new maximum value

bool bool tf::Semaphore::_try_acquire_or_wait (Node *) _try_acquire_or_wait tf::Semaphore::_try_acquire_or_wait Node * me
void void tf::Semaphore::_release (SmallVector< Node * > &) _release tf::Semaphore::_release SmallVector< Node * > & dst

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::Executorexecutor(8);//createanexecutorof8workers
    tf::Taskflowtaskflow;

    tf::Semaphoresemaphore(1);//createasemaphorewithinitialcount1

    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){//eachtaskacquiresandreleasethesemaphore
    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.

- tf::Semaphore
  _cur_value
- tf::Semaphore
  _max_value
- tf::Semaphore
  _mtx
- tf::Semaphore
  _release
- tf::Semaphore
  _try_acquire_or_wait
- tf::Semaphore
  _waiters
- tf::Semaphore
  Executor
- tf::Semaphore
  max_value
- tf::Semaphore
  Node
- tf::Semaphore
  reset
- tf::Semaphore
  reset
- tf::Semaphore
  Semaphore
- tf::Semaphore
  Semaphore
- tf::Semaphore
  value