cppthread 1.1.16
C++ Thread Library
thread.h
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
32// self
33//
34#include <cppthread/mutex.h>
35
36
37
38// libexcept
39//
40#include <libexcept/scoped_signal_mask.h>
41
42
43// C++
44//
45#include <functional>
46#include <string>
47#include <vector>
48
49
50
51namespace cppthread
52{
53
54
55enum class leave_status_t;
56class runner;
57
58
59constexpr pid_t PID_UNDEFINED = static_cast<pid_t>(-1);
60constexpr pthread_t THREAD_UNDEFINED = static_cast<pthread_t>(-1);
61
62
63class thread
64{
65public:
66 typedef std::shared_ptr<thread> pointer_t;
67 typedef std::vector<pointer_t> vector_t;
68
69 thread(std::string const & name, runner * runner);
70 thread(std::string const & name, std::shared_ptr<runner> runner);
71 thread(thread const & rhs) = delete;
72 ~thread();
73
74 thread & operator = (thread const & rhs) = delete;
75
76 std::string const & get_name() const;
77 runner * get_runner() const;
78 bool is_running() const;
79 bool is_stopping() const;
80 bool start();
81 void stop(std::function<void(thread *)> callback = nullptr);
82 bool kill(int sig);
83 void mask_signals(libexcept::sig_list_t list);
84 void mask_all_signals();
85 void unmask_signals(libexcept::sig_list_t list);
86 pid_t get_thread_tid() const;
87 mutex & get_thread_mutex() const;
88 void set_log_all_exceptions(bool log_all_exceptions);
89 bool get_log_all_exceptions() const;
90 std::exception_ptr get_exception() const;
91
92private:
93 void init();
94
95 // internal function to start the runner
96 friend void * func_internal_start(void * system_thread);
97 void internal_thread();
98 bool internal_enter();
99 bool internal_run();
100 void internal_leave(leave_status_t status);
101
102 std::string const f_name = std::string();
103 runner * f_runner = nullptr;
104 mutable mutex f_mutex = mutex();
105 bool f_running = false;
106 bool f_started = false;
107 bool f_stopping = false;
111 pthread_attr_t f_thread_attr = pthread_attr_t();
112 std::exception_ptr f_exception = std::exception_ptr();
113};
114
115
118pid_t gettid();
119pid_t get_pid_max();
120int set_current_thread_name(std::string const & name);
121int set_thread_name(pid_t tid, std::string const & name);
122std::string get_current_thread_name();
123std::string get_thread_name(pid_t tid);
124
125typedef std::vector<pid_t> process_ids_t;
126
127process_ids_t get_thread_ids(pid_t pid = -1);
128bool is_process_running(pid_t pid);
129std::string get_boot_id();
130std::size_t get_thread_count();
131bool is_using_vdso();
132int deswappify(pid_t pid);
133
134
135} // namespace cppthread
136// vim: ts=4 sw=4 et
A mutex object to ensures atomicity.
Definition mutex.h:55
The runner is the class that wraps the actual system thread.
Definition runner.h:65
A thread object that ensures proper usage of system threads.
Definition thread.h:64
void mask_all_signals()
Block all signals for this thread.
Definition thread.cpp:927
bool f_stopping
The thread is currently in the stopping process.
Definition thread.h:107
void mask_signals(libexcept::sig_list_t list)
Masks signals from this thread.
Definition thread.cpp:892
thread(thread const &rhs)=delete
The copy operator is deleted.
thread & operator=(thread const &rhs)=delete
The assignment operator is deleted.
pid_t f_tid
This thread identifier.
Definition thread.h:109
std::exception_ptr f_exception
An exception pointer.
Definition thread.h:112
pthread_attr_t f_thread_attr
This thread attributes.
Definition thread.h:111
std::shared_ptr< thread > pointer_t
The shared pointer for a thread object.
Definition thread.h:66
void set_log_all_exceptions(bool log_all_exceptions)
Whether to log exceptions caught in the thread.
Definition thread.cpp:775
void init()
This private function initializes the thread.
Definition thread.cpp:121
runner * get_runner() const
Get a pointer to this thread runner.
Definition thread.cpp:231
std::string const f_name
The name of this thread.
Definition thread.h:102
bool is_running() const
Check whether the thread is considered to be running.
Definition thread.cpp:253
bool internal_run()
Execute the run() function.
Definition thread.cpp:483
bool get_log_all_exceptions() const
Retrieve whether all exceptions get logged or not.
Definition thread.cpp:803
friend void * func_internal_start(void *system_thread)
Start the actual thread.
Definition thread.cpp:293
void internal_thread()
Run the thread process.
Definition thread.cpp:318
std::vector< pointer_t > vector_t
A vector of threads.
Definition thread.h:67
mutex f_mutex
The thread mutex to guard various functions.
Definition thread.h:104
runner * f_runner
The actual thread object.
Definition thread.h:103
void stop(std::function< void(thread *)> callback=nullptr)
Stop the thread.
Definition thread.cpp:656
bool is_stopping() const
Check whether the thread was asked to stop.
Definition thread.cpp:269
std::exception_ptr get_exception() const
Get the exception pointer.
Definition thread.cpp:836
pthread_t f_thread_id
This thread identifier.
Definition thread.h:110
~thread()
Delete a thread object.
Definition thread.cpp:175
void unmask_signals(libexcept::sig_list_t list)
Unmask signals for this thread.
Definition thread.cpp:947
bool f_started
The thread is started.
Definition thread.h:106
std::string const & get_name() const
Retrieve the name of this process object.
Definition thread.cpp:209
bool f_running
The thread is running.
Definition thread.h:105
mutex & get_thread_mutex() const
Retrieve a reference to the thread mutex.
Definition thread.cpp:740
bool internal_enter()
Enter the thread runner.
Definition thread.cpp:434
void internal_leave(leave_status_t status)
Function called when leaving the thread runner.
Definition thread.cpp:528
bool start()
Attempt to start the thread.
Definition thread.cpp:575
pid_t get_thread_tid() const
Retrieve the thread identifier of this thread.
Definition thread.cpp:726
bool f_log_all_exceptions
Whether the runner exceptions should be logged or not.
Definition thread.h:108
bool kill(int sig)
Send a signal to this thread.
Definition thread.cpp:862
int deswappify(pid_t pid)
Go through the swapped out sections of a process and deswappify them.
Thread Runner and Managers.
leave_status_t
The exit status.
Definition runner.h:53
bool is_process_running(pid_t pid)
Check whether a process is running or not.
Definition thread.cpp:1344
pid_t gettid()
Get the thread identifier of the current thread.
Definition thread.cpp:1047
std::size_t get_thread_count()
Get the number of threads still not joined in this process.
Definition thread.cpp:1399
int set_current_thread_name(std::string const &name)
Set the name of the currently running thread.
Definition thread.cpp:1161
int set_thread_name(pid_t tid, std::string const &name)
Set the name of the specified thread.
Definition thread.cpp:1194
process_ids_t get_thread_ids(pid_t pid)
Retrieve the list of threads for a given process.
Definition thread.cpp:1278
bool is_using_vdso()
Certain functions may be implemented using the vDSO library.
Definition thread.cpp:1431
pid_t get_pid_max()
Get the maximum process identifier.
Definition thread.cpp:1129
int get_total_number_of_processors()
Retrieve the number of processors available on this system.
Definition thread.cpp:979
std::string get_boot_id()
Retrieve the boot UUID.
Definition thread.cpp:1372
std::string get_thread_name(pid_t tid)
Retrieve the name of a thread.
Definition thread.cpp:1259
int get_number_of_available_processors()
Retrieve the number of processors currently usable.
Definition thread.cpp:1031
std::string get_current_thread_name()
Retrieve the name of the current thread.
Definition thread.cpp:1236
std::vector< pid_t > process_ids_t
A list of identifiers representing various processes.
Definition thread.h:125
constexpr pid_t PID_UNDEFINED
The value a PID variable is set to when not representing a process.
Definition thread.h:59
constexpr pthread_t THREAD_UNDEFINED
An invalid thread identifier for initialization.
Definition thread.h:60

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.