Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/counting semaphore"

From cppreference.com
< cpp‎ | thread
(~ fmt, add delay between release/acquire, 10s → 3s .)
m (yuck, don't detach threads. Also only needs one flush (before the pause), Also, milliseconds->ms, and since we're in 20 land, thread->jthread. Scheduling by injecting delays is also really bad, but another time.)
Line 53: Line 53:
 
#include <chrono>
 
#include <chrono>
 
#include <semaphore>
 
#include <semaphore>
 +
using namespace std::literals;
  
 
// global binary semaphore instance
 
// global binary semaphore instance
 
// object's count is set to zero
 
// object's count is set to zero
 
// object is in non-signaled state
 
// object is in non-signaled state
 
 
std::binary_semaphore smphSignal(0);
 
std::binary_semaphore smphSignal(0);
 
+
 
void ThreadProc()
 
void ThreadProc()
 
{
 
{
Line 65: Line 65:
 
     // by attempting to decrement the semaphore
 
     // by attempting to decrement the semaphore
 
     smphSignal.acquire();
 
     smphSignal.acquire();
 
+
 
     // this call blocks until the semaphore's count
 
     // this call blocks until the semaphore's count
 
     // is increased from the main proc
 
     // is increased from the main proc
 
+
 
     std::cout << "[thread] Got the signal" << std::endl; // response message
 
     std::cout << "[thread] Got the signal" << std::endl; // response message
 
+
 
     // wait for 3 seconds to imitate some work
 
     // wait for 3 seconds to imitate some work
 
     // being done by the thread
 
     // being done by the thread
     std::this_thread::sleep_for(std::chrono::seconds(3));
+
     std::this_thread::sleep_for(3s);
 
+
     std::cout << "[thread] Send the signal" << std::endl; // message
+
     std::cout << "[thread] Send the signal\n"; // message
 
+
 
     // signal the main proc back
 
     // signal the main proc back
 
     smphSignal.release();
 
     smphSignal.release();
 
}
 
}
 
+
 
int main()
 
int main()
 
{
 
{
 
     // create some background worker thread
 
     // create some background worker thread
 
     // that would exist for a long period of time
 
     // that would exist for a long period of time
     std::thread thrWorker(ThreadProc);
+
     std::jthread thrWorker(ThreadProc);
    thrWorker.detach();
+
 
+
     std::cout << "[main] Send the signal\n"; // message
     std::cout << "[main] Send the signal" << std::endl; // message
+
 
+
 
     // signal the worker thread to start working
 
     // signal the worker thread to start working
 
     // by increasing the semaphore's count
 
     // by increasing the semaphore's count
 
     smphSignal.release();
 
     smphSignal.release();
 
+
 
     // release() followed by acquire() may prevent the worker
 
     // release() followed by acquire() may prevent the worker
 
     // thread from acquiring the semaphore, so add some delay:
 
     // thread from acquiring the semaphore, so add some delay:
     std::this_thread::sleep_for(std::chrono::milliseconds(50));
+
     std::this_thread::sleep_for(50ms);
 
+
 
     // wait until the worker thread is done doing the work
 
     // wait until the worker thread is done doing the work
 
     // by attempting to decrement the semaphore's count
 
     // by attempting to decrement the semaphore's count
 
     smphSignal.acquire();
 
     smphSignal.acquire();
 
+
     std::cout << "[main] Got the signal" << std::endl; // response message
+
     std::cout << "[main] Got the signal\n"; // response message
 
}
 
}
  | output= <!-- work with clang-11 -->
+
  | output=
 
[main] Send the signal
 
[main] Send the signal
 
[thread] Got the signal
 
[thread] Got the signal

Revision as of 10:09, 28 October 2020

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
counting_semaphorebinary_semaphore
(C++20)(C++20)
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <semaphore>
template<std::ptrdiff_t LeastMaxValue = /* implementation-defined */>
class counting_semaphore;
(1) (since C++20)
using binary_semaphore = std::counting_semaphore<1>;
(2) (since C++20)
1) A counting_semaphore is a lightweight synchronization primitive that can control access to a shared resource. Unlike a std::mutex, a counting_semaphore allows more than one concurrent access to the same resource, for at least LeastMaxValue concurrent accessors. The program is ill-formed if LeastMaxValue is negative.
2) binary_semaphore is an alias for specialization of std::counting_semaphore with LeastMaxValue being 1. Implementations may implement binary_semaphore more efficiently than the default implementation of std::counting_semaphore.

A counting_semaphore contains an internal counter initialized by the constructor. This counter is decremented by calls to acquire() and related methods, and is incremented by calls to release(). When the counter is zero, acquire() blocks until the counter is incremented, but try_acquire() does not block; try_acquire_for() and try_acquire_until() block until the counter is incremented or a timeout is reached.

Similar to std::condition_variable's wait(), counting_semaphore's try_acquire() can spuriously fail.

Specializations of std::counting_semaphore are not DefaultConstructible, CopyConstructible, MoveConstructible, CopyAssignable, or MoveAssignable.

Contents

Member functions

constructs a counting_semaphore
(public member function) [edit]
destructs the counting_semaphore
(public member function) [edit]
operator=
[deleted]
counting_semaphore is not assignable
(public member function) [edit]
Operations
increments the internal counter and unblocks acquirers
(public member function) [edit]
decrements the internal counter or blocks until it can
(public member function) [edit]
tries to decrement the internal counter without blocking
(public member function) [edit]
tries to decrement the internal counter, blocking for up to a duration time
(public member function) [edit]
tries to decrement the internal counter, blocking until a point in time
(public member function) [edit]
Constants
[static]
returns the maximum possible value of the internal counter
(public static member function) [edit]

Notes

As its name indicates, the LeastMaxValue is the minimum max value, not the actual max value. Thus max() can yield a number larger than LeastMaxValue.

Unlike std::mutex a counting_semaphore is not tied to threads of execution - acquiring a semaphore can occur on a different thread than releasing the semaphore, for example. All operations on counting_semaphore can be performed concurrently and without any relation to specific threads of execution, with the exception of the destructor which cannot be performed concurrently but can be performed on a different thread.

Semaphores are also often used for the semantics of signalling/notifying rather than mutual exclusion, by initializing the semaphore with 0 and thus blocking the receiver(s) that try to acquire(), until the notifier "signals" by invoking release(n). In this respect semaphores can be considered alternatives to std::condition_variables, often with better performance.

Example

#include <iostream>
#include <thread>
#include <chrono>
#include <semaphore>
using namespace std::literals; 
 
// global binary semaphore instance
// object's count is set to zero
// object is in non-signaled state
std::binary_semaphore smphSignal(0);
 
void ThreadProc()
{
    // wait for a signal from the main proc
    // by attempting to decrement the semaphore
    smphSignal.acquire();
 
    // this call blocks until the semaphore's count
    // is increased from the main proc
 
    std::cout << "[thread] Got the signal" << std::endl; // response message
 
    // wait for 3 seconds to imitate some work
    // being done by the thread
    std::this_thread::sleep_for(3s);
 
    std::cout << "[thread] Send the signal\n"; // message
 
    // signal the main proc back
    smphSignal.release();
}
 
int main()
{
    // create some background worker thread
    // that would exist for a long period of time
    std::jthread thrWorker(ThreadProc);
 
    std::cout << "[main] Send the signal\n"; // message
 
    // signal the worker thread to start working
    // by increasing the semaphore's count
    smphSignal.release();
 
    // release() followed by acquire() may prevent the worker
    // thread from acquiring the semaphore, so add some delay:
    std::this_thread::sleep_for(50ms);
 
    // wait until the worker thread is done doing the work
    // by attempting to decrement the semaphore's count
    smphSignal.acquire();
 
    std::cout << "[main] Got the signal\n"; // response message
}

Output:

[main] Send the signal
[thread] Got the signal
[thread] Send the signal
[main] Got the signal