class::sys::Sem

class sys::Sem
  : public api::WorkObject

This class is a wrapper for POSIX style semphores.

Here is an example using named semaphores.

#include <sapi/sys.hpp>

Sem sem;
sem.create("my_semaphore", 10);
sem.wait(); //decrement the semaphore value, block if semaphore can't be decremented
sem.post(); //increment semaphore value
sem.close(); //close reference to semaphore
sem.unlink(); //delete semaphore if all references are closed

Summary

Sem()

int close()

int create(

const char * name,
int value,
bool exclusive
)

int destroy()

int get_value() const

int init(

sem_t * sem,
int pshared,
unsigned int value
)

int open(

const char * name,
int o_flags,
int mode,
int value
)

int post()

int timedwait(

const chrono::ClockTime & clock_time
)

int try_wait()

int wait()

int wait_timed(

const chrono::ClockTime & clock_time
)

static int unlink(

const char * name
)

enum @21

Members

Sem()

int close()

Closes a named semaphore.

See also: open()


int create(

const char * name,
int value,
bool exclusive
)

Creates a new named semaphore.

Parameters

  • name The name of the semaphore

  • value The value assigned to the semaphore

  • exclusive If true, the method will fail if the semaphore already exists. If false, it will open the existing semaphore.

Returns

Zero on success or less than zero with errno set


int destroy()

Destroys an unnamed semaphore.

This method is used in conjuntion with init() not open().


int get_value() const

Gets the semaphore value.

Returns

The value of the semaphore (>= 0) or -1 on an error


int init(

sem_t * sem,
int pshared,
unsigned int value
)

Initializes an unnamed semaphore.

Parameters

  • sem A pointer to the semaphore

  • pshared Whether or not semaphore is shared between another process

  • value The value of the semaphore

Returns

Zero on success

See also: destroy()


int open(

const char * name,
int o_flags,
int mode,
int value
)

Opens a named semaphore

Parameters

  • name The name of the semaphore

  • o_flags Flags used to open semaphore

  • mode The access mode permissions for the semaphore

  • value The value of the semaphore (used when creating)

Returns

Zero on success

See also: close()


int post()

Increments the semaphore value allowing another thread/process to use the semaphore.

Returns

Zero on success


int timedwait(

const chrono::ClockTime & clock_time
)

Equivalent to wait_timed() but follow the posix naming.


int try_wait()

Checks to see if semaphore is available.


int wait()

Waits for the semaphore to become available.

This method will block until the semaphore becomes available and decrement the semaphore value. The post() method will release (increment the value of) the semaphore.


int wait_timed(

const chrono::ClockTime & clock_time
)

Executes a timed wait for the semaphore.

Parameters

  • clock_time The amount of time to wait before timing out

Returns

Zero on success, less than zero for an error or timeout

This method will block until either the semaphore becomes available and is decremented or until the timeout is exceeded.


const char * name
)

Deletes a named semaphore.

Parameters

  • name The name of the semaphore

enum @21
  • CREATE Create Semaphore flag
  • EXCL Create exclusive
  • EXCLUSIVE Create exclusive

X

Thanks for Coming!

Subscribe to news and updates