STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Public Member Functions | List of all members
stt::system::csemp Class Reference

Synchronization tool class encapsulating System V semaphores. More...

#include <sttnet_English.h>

Public Member Functions

 csemp ()
 Constructor, initializes internal state. More...
 
bool init (key_t key, unsigned short value=1, short sem_flg=SEM_UNDO)
 Initialize the semaphore. More...
 
bool wait (short value=-1)
 P operation (wait), attempts to subtract value from the semaphore. More...
 
bool post (short value=1)
 V operation (release), attempts to add value to the semaphore. More...
 
int getvalue ()
 Get the current value of the semaphore. More...
 
bool destroy ()
 Destroy the current semaphore. More...
 
 ~csemp ()
 Destructor, does not automatically destroy the semaphore. More...
 

Detailed Description

Synchronization tool class encapsulating System V semaphores.

csemp provides a mutual exclusion mechanism, supporting inter-process synchronization operations. By encapsulating system calls such as semget, semop, semctl, it implements semaphore initialization, P (wait), V (release), destruction, and current value reading.

Copy construction and assignment operators are disabled to ensure resource uniqueness.

Constructor & Destructor Documentation

stt::system::csemp::csemp ( )
inline

Constructor, initializes internal state.

stt::system::csemp::~csemp ( )

Destructor, does not automatically destroy the semaphore.

Member Function Documentation

bool stt::system::csemp::destroy ( )

Destroy the current semaphore.

Generally used to clean up resources before the main process holding the semaphore exits.

Returns
true on success; false on failure.
int stt::system::csemp::getvalue ( )

Get the current value of the semaphore.

Returns
The value of the semaphore; -1 on failure.
bool stt::system::csemp::init ( key_t  key,
unsigned short  value = 1,
short  sem_flg = SEM_UNDO 
)

Initialize the semaphore.

If the semaphore already exists, it is obtained; otherwise, an attempt is made to create and set the initial value.

Parameters
keyUnique key of the semaphore.
valueInitial value of the semaphore (default 1, representing a mutex).
sem_flgSemaphore operation flags, default SEM_UNDO, operations are automatically revoked when the process terminates.
Returns
true on success; false on failure.
bool stt::system::csemp::post ( short  value = 1)

V operation (release), attempts to add value to the semaphore.

Releases resources or wakes up waiting processes.

Parameters
valueRelease value (must be greater than 0, default 1).
Returns
true on success; false on failure.
bool stt::system::csemp::wait ( short  value = -1)

P operation (wait), attempts to subtract value from the semaphore.

If the current value is insufficient, it will block until available.

Parameters
valueWait value (must be less than 0, default -1).
Returns
true on success; false on failure.

The documentation for this class was generated from the following file: