cppthread 1.1.16
C++ Thread Library
runner.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// C++
38//
39#include <string>
40#include <vector>
41
42
43
44namespace cppthread
45{
46
47
48
49class thread;
50
51
53{
54 LEAVE_STATUS_NORMAL,
55 LEAVE_STATUS_INITIALIZATION_FAILED,
56 LEAVE_STATUS_THREAD_FAILED,
57 LEAVE_STATUS_INSTRUMENTATION,
58};
59
60
61// this is the actual thread because we cannot use the main thread
62// object destructor to properly kill a thread in a C++ environment
63//
64class runner
65{
66public:
67 typedef std::shared_ptr<runner> pointer_t;
68 typedef std::vector<pointer_t> vector_t;
69
70 runner(std::string const & name);
71 runner(runner const & rhs) = delete;
72 virtual ~runner();
73
74 runner & operator = (runner const & rhs) = delete;
75
76 std::string const & get_name() const;
77 virtual bool is_ready() const;
78 virtual bool continue_running() const;
79 virtual void enter();
80 virtual void run() = 0;
81 virtual void leave(leave_status_t status);
82 thread * get_thread() const;
83 pid_t gettid() const;
84
85protected:
86 mutable mutex f_mutex = mutex();
87
88private:
96 friend class thread;
97
98 thread * f_thread = nullptr;
99 std::string const f_name = std::string();
100};
101
102
103
104
105
106} // namespace cppthread
107// 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
std::shared_ptr< runner > pointer_t
The shared pointer of a thread runner.
Definition runner.h:67
std::vector< pointer_t > vector_t
A vector of threads.
Definition runner.h:68
virtual bool continue_running() const
Whether the thread should continue running.
Definition runner.cpp:153
virtual ~runner()
The destructor checks that the thread was stopped.
Definition runner.cpp:78
mutex f_mutex
The mutex of this thread.
Definition runner.h:86
runner(runner const &rhs)=delete
The copy operator is deleted.
std::string const f_name
The name of this thread.
Definition runner.h:99
virtual void enter()
Signal that the run() function is about to be entered.
Definition runner.cpp:183
virtual void run()=0
This virtual function represents the code run by the thread.
thread * get_thread() const
Retrieve the thread controller linked to this runner.
Definition runner.cpp:258
std::string const & get_name() const
Retrieve the name of the runner.
Definition runner.cpp:106
virtual void leave(leave_status_t status)
Signal that the run() function has returned.
Definition runner.cpp:231
thread * f_thread
A pointer back to the owner ("parent") of this runner.
Definition runner.h:98
virtual bool is_ready() const
Check whether this thread runner is ready.
Definition runner.cpp:120
pid_t gettid() const
Get this runner thread identifier.
Definition runner.cpp:274
runner & operator=(runner const &rhs)=delete
This assignment operator is deleted.
A thread object that ensures proper usage of system threads.
Definition thread.h:64
Thread Runner and Managers.
leave_status_t
The exit status.
Definition runner.h:53

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.