tf::Semaphore taskflow/core/semaphore.hpp class friend class Node Node Node std::atomic< size_t > std::atomic<size_t> tf::Semaphore::_count _count tf::Semaphore::Semaphore () Semaphore constructs a default semaphore with count equal to zero Application can use tf::Semaphore::reset to reset the counter of the semaphore later. tf::Semaphore::Semaphore (size_t count) Semaphore size_t count constructs a semaphore with the given count A semaphore creates a constraint that limits the maximum concurrency, i.e., the number of workers, in a set of tasks. tf::Semaphoresemaphore(4);//asemaphoreinitializedwith4 size_t size_t tf::Semaphore::count (std::memory_order memory_order=std::memory_order_seq_cst) const count std::memory_order memory_order std::memory_order_seq_cst queries the current value of the associated counter memory_order the memory order of this load (default std::memory_order_seq_cst) Queries the current value of the associated counter. bool bool tf::Semaphore::try_acquire () try_acquire tries to atomically decrement the internal counter by 1 if it is greater than 0 true if it decremented the internal counter, otherwise false Tries to atomically decrement the internal counter by 1. If the operation succeeds, returns true, otherwise false. void void tf::Semaphore::release (size_t n=1) release size_t n 1 atomically increment the internal counter by n n the value by which the internal counter will be incremented The release operation always succeeds as it simply increments the counter of this semaphore. void void tf::Semaphore::reset (size_t count, std::memory_order memory_order=std::memory_order_seq_cst) reset size_t count std::memory_order memory_order std::memory_order_seq_cst resets the semaphore to the given count count the new count value memory_order memory order to which this operation will be applied (default std::memory_order_seq_cst) Calling tf::Semaphore::reset will immediately change the underlying counter to the given count value, regardless other threads acquiring or releasing the semaphore. 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 for(size_ti=0;i<1000;i++){ taskflow.emplace([&](tf::Runtime&rt){ rt.acquire(semaphore); std::cout<<"criticalsectionhere(oneworkeratanytime)\n"; critical_section(); rt.release(semaphore); }); } executor.run(taskflow).wait(); The above example creates a taskflow of 1000 independent tasks while only one worker will run critical_section at any time due to the semaphore constraint. This arrangement limits the parallelism of critical_section to just one. Taskflow use a non-blocking algorithm to implement the acquisition of semaphores and thus is deadlock-free. tf::Semaphore_count tf::Semaphorecount tf::SemaphoreNode tf::Semaphorerelease tf::Semaphorereset tf::SemaphoreSemaphore tf::SemaphoreSemaphore tf::Semaphoretry_acquire