Loading...
Searching...
No Matches
thread.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_THREAD_HPP
#define LIBTHREADAR_THREAD_HPP
#include "config.h"
// C system headers
extern "C"
{
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
}
// C++ standard headers
// libthreadar headers
#include "mutex.hpp"
namespace libthreadar
{
class thread
{
public:
thread();
thread(const thread & ref) = delete;
thread(thread && ref) noexcept = default;
thread & operator = (const thread & ref) = delete;
thread & operator = (thread && ref) noexcept = default;
virtual ~thread();
void reset_stack_size();
void set_stack_size(unsigned int val);
unsigned int get_stack_size() const { return stack_size; };
virtual void set_signal_mask(const sigset_t & mask) { sigmask = mask; };
void run();
bool is_running() const { return running; };
bool is_running(pthread_t & id) const;
void join() const;
void kill() const;
void cancel();
protected:
class cancel_except
{
public:
cancel_except() {};
cancel_except(cancel_except &) = default;
cancel_except(cancel_except &&) noexcept = default;
cancel_except & operator = (cancel_except &) = default;
cancel_except & operator = (cancel_except &&) noexcept = default;
~cancel_except() = default;
};
virtual void inherited_run() = 0;
void cancellation_checkpoint() const;
virtual void inherited_cancel() {};
private:
mutable mutex field_control;
bool running;
pthread_t tid;
bool joignable;
mutable bool do_cancel;
sigset_t sigmask;
unsigned int stack_size;
char* stack;
void clear_stack();
// static members
static void *run_obj(void *obj); //< called by pthread_create to spawn a new thread
};
} // end of namespace
#endif
libthreadar::mutex
Wrapper around the Posix pthread_mutex_t C objects.
Definition mutex.hpp:57
libthreadar::thread::cancel_except
exception used to trigger thread cancellation
Definition thread.hpp:182
libthreadar::thread
Class thread is a pure virtual class, that implements thread creation and operations.
Definition thread.hpp:102
libthreadar::thread::is_running
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object
Definition thread.hpp:146
libthreadar::thread::reset_stack_size
void reset_stack_size()
reset the stack size to the system default value
libthreadar::thread::cancellation_checkpoint
void cancellation_checkpoint() const
available withing the inherited_run() method to eventually trigger thread cancellation
libthreadar::thread::set_signal_mask
virtual void set_signal_mask(const sigset_t &mask)
set signal mask of the thread spawn by run()
Definition thread.hpp:140
libthreadar::thread::run
void run()
launch the current object routing in a separated thread
libthreadar::thread::inherited_cancel
virtual void inherited_cancel()
Definition thread.hpp:251
libthreadar::thread::get_stack_size
unsigned int get_stack_size() const
get the current stack size value
Definition thread.hpp:134
libthreadar::thread::kill
void kill() const
libthreadar::thread::cancel
void cancel()
the caller send a cancellation request to this object's running thread if any
libthreadar::thread::inherited_run
virtual void inherited_run()=0
action to be performed in the separated thread (implementation is expected in inherited classes)
libthreadar::thread::is_running
bool is_running(pthread_t &id) const
checks whether the object is running in a separated thread
libthreadar::thread::thread
thread(const thread &ref)=delete
copy constructor and assignment operator are disabled
libthreadar::thread::set_stack_size
void set_stack_size(unsigned int val)
set the stack size to non default value
libthreadar::thread::~thread
virtual ~thread()
destructor
libthreadar::thread::thread
thread()
constructor
libthreadar::thread::join
void join() const
the caller will be suspended until the current object's thread ends
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