Loading...
Searching...
No Matches
semaphore.hpp
Go to the documentation of this file.
/*********************************************************************/
// libthreadar - is a library providing several C++ classes to work with threads
// Copyright (C) 2014-2025 Denis Corbin
//
// This file is part of libthreadar
//
// libthreadar is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libhtreadar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libthreadar. If not, see <http://www.gnu.org/licenses/>
//
//----
// to contact the author: dar.linux@free.fr
/*********************************************************************/
#ifndef LIBTHREADAR_SEMAPHORE_HPP
#define LIBTHREADAR_SEMAPHORE_HPP
#include "config.h"
// C system headers
extern "C"
{
}
// C++ standard headers
#include <string>
// libthreadar headers
#include "mutex.hpp"
namespace libthreadar
{
class semaphore
{
public:
semaphore(unsigned int max_value);
semaphore(const semaphore & ref) = delete;
semaphore(semaphore && ref) noexcept = default;
semaphore & operator = (const semaphore & ref) = delete;
semaphore & operator = (semaphore && ref) noexcept = default;
~semaphore();
bool waiting_thread() const;
bool working_thread() const;
void lock();
void unlock();
void reset();
int get_value() const { return value; };
private:
int value; //< this is the semaphore value
mutex val_mutex; //< this controls modification to value
mutex semaph; //< this mutex is used to suspend thread semaphore value get negative
int max_value; //< maximum value the semaphore cannot exceed
};
} // end of namespace
#endif
libthreadar::mutex
Wrapper around the Posix pthread_mutex_t C objects.
Definition mutex.hpp:57
libthreadar::semaphore
Class semaphore is an enhanced version of Posix semaphore.
Definition semaphore.hpp:53
libthreadar::semaphore::semaphore
semaphore(const semaphore &ref)=delete
no copy constructor
libthreadar::semaphore::working_thread
bool working_thread() const
return whether the semaphore has at least one thread that acquired the lock, possibily without other ...
libthreadar::semaphore::semaphore
semaphore(unsigned int max_value)
semaphore constuctor
libthreadar::semaphore::~semaphore
~semaphore()
Destructor.
libthreadar::semaphore::semaphore
semaphore(semaphore &&ref) noexcept=default
no move constructor
libthreadar::semaphore::lock
void lock()
Request a "resource".
libthreadar::semaphore::get_value
int get_value() const
Return the value of the semaphore, that's to say the number of available "resources".
Definition semaphore.hpp:111
libthreadar::semaphore::waiting_thread
bool waiting_thread() const
Return whether the semaphore has at least a pending thread waiting for another thread to unlock it.
libthreadar::semaphore::reset
void reset()
Reset to initial state releasing any thread that could wait on the semaphore.
libthreadar::semaphore::operator=
semaphore & operator=(const semaphore &ref)=delete
no assignment operator
libthreadar::semaphore::unlock
void unlock()
Release a "resource".
mutex.hpp
defines the mutex C++ class
libthreadar
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition barrier.hpp:46