Skip to content

timed_semaphore

Alairion edited this page May 8, 2021 · 7 revisions

Name

Defined in header <nes/semaphore.hpp>

class timed_semaphore;

Description

nes::timed_semaphore is a synchronization primitive that use a resource counter. When trying to acquire a resource, the calling thread is blocked until a resource is available. If you don't want to block a thread on acquire, then use try_acquire instead. You may also use try_acquire_for or try_acquire_until if you want to wait for a specific amount of time or until a time point has been reached, respectively. To make a resource available, you have to increment the timed_semaphore counter by calling release. All member functions are thread-safe.

Member types

Type Description
native_handle_type The underlying, implementation-defined, representation of a process

Public Member functions

Function Description
timed_semaphore Constructs the timed_semaphore object
~timed_semaphore Destroys the timed_semaphore object
operator= Deleted
acquire Waits until a resource is available, then decrements the resource counter
try_acquire Checks if a resource is available, and if that is the case decrements the resource counter
try_acquire_for Checks if a resource is available within a certain delay, and if that is the case decrements the resource counter
try_acquire_until Checks if a resource is available until a specified time point, and if that is the case decrements the resource counter
release Increments the resource counter
native_handle Returns the underlying, implementation-defined, representation of the timed_semaphore

Example

#include <thread>
#include <iostream>
#include <array>

#include <nes/semaphore.hpp>

void another_thread(const std::array<std::uint64_t, 8>& data, nes::timed_semaphore& timed_semaphore)
{
    for(std::size_t i{}; i < 8; ++i)
    {
        std::uint32_t count{};
        //Wait until the next resource is available
        while(!timed_semaphore.try_acquire_for(std::chrono::milliseconds{10}))
            ++count;

        //It is ready, display it
        std::cout << "Resource acquired after " << count << " tries : " << data[i] << std::endl;
    }
}

int main()
{
    std::array<std::uint64_t, 8> data{0, 1};
    //Two resources are already available, so we create a timed_semaphore with two resource
    nes::timed_semaphore timed_semaphore{2};

    std::thread thread{another_thread, std::cref(data), std::ref(timed_semaphore)};

    for(std::size_t i{2}; i < 8; ++i)
    {
        //Simulate long processing time
        std::this_thread::sleep_for(std::chrono::milliseconds{250});
        //Compute something
        data[i] = i * i;
        //Increment the resource counter of the timed_semaphore (unlock the other thread only once)
        timed_semaphore.release();
    }

    if(thread.joinable())
        thread.join();
}

Output

Resource acquired after 0 tries : 0
Resource acquired after 0 tries : 1
Resource acquired after 24 tries : 4
Resource acquired after 23 tries : 9
Resource acquired after 23 tries : 16
Resource acquired after 23 tries : 25
Resource acquired after 23 tries : 36
Resource acquired after 23 tries : 49
Clone this wiki locally