NikolasK-source / cxxsemaphore

A C++ library to handle POSIX named semaphores

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CxxSemaphore

A C++ Library to handle POSIX named Semaphores.

Usage

Initialize cxxsemaphore::Semaphore

Option A: Create Semaphore

const std::string sem_name     = "example_sem";  // semaphore name
constexpr unsigned sem_value   = 1;              // initial semaphore value
constexpr bool force           = false;          // fail if semaphore already exists

// create semaphore
cxxsemaphore::Semaphore sem(sem_name, sem_value, force);

Option B: Connect to Existing Semaphore

const std::string sem_name = "example_sem";  // semaphore name

// use semaphore
cxxsemaphore::Semaphore sem(shm_name, read_only);

Wait for Semaphore

Wait forever

sem.wait();

Wait with Timeout (sec, nsec)

bool success = sem.wait({2, 0});

Try to acquire semaphore

bool success = sem.try_wait();

Release the Semaphore

sem.post()

About

A C++ library to handle POSIX named semaphores

License:MIT License


Languages

Language:CMake 67.4%Language:C++ 24.1%Language:Shell 8.5%