File: multithreaded_object_extension_abstract.h

package info (click to toggle)
concurrentqueue 1.0.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,648 kB
  • sloc: cpp: 37,303; makefile: 88; ansic: 67; python: 46; sh: 18
file content (186 lines) | stat: -rw-r--r-- 6,210 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2007  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_MULTITHREADED_OBJECT_EXTENSIOn_ABSTRACT_
#ifdef DLIB_MULTITHREADED_OBJECT_EXTENSIOn_ABSTRACT_ 

#include "threads_kernel_abstract.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    class multithreaded_object
    {
        /*!
            INITIAL VALUE
                - is_running() == false
                - number_of_threads_alive() == 0
                - number_of_threads_registered() == 0

            WHAT THIS OBJECT REPRESENTS
                This object represents a multithreaded object.  It is similar to 
                the threaded_object except it allows you to have many threads in a 
                single object rather than just one.  To use it you inherit from it 
                and register the member functions in your new class that you want 
                to run in their own threads by calling register_thread().  Then when 
                you call start() it will spawn all the registered functions
                in their own threads.
        !*/

    public:

        multithreaded_object (
        );
        /*!
            ensures
                - #*this is properly initialized
            throws
                - std::bad_alloc
                - dlib::thread_error
                    the constructor may throw this exception if there is a problem 
                    gathering resources to create threading objects.
        !*/

        virtual ~multithreaded_object (
        ) = 0;
        /*!
            requires
                - number_of_threads_alive() == 0
                  (i.e. in the destructor for the object you derive from this one you
                  must wait for all the threads to end.)
            ensures
                - all resources allocated by *this have been freed.  
        !*/

        void clear(
        );
        /*!
            ensures
                - #*this has its initial value
                - blocks until all threads have terminated
            throws
                - std::bad_alloc or dlib::thread_error
                    if an exception is thrown then *this is unusable 
                    until clear() is called and succeeds
        !*/

        bool is_running (
        ) const;
        /*!
            ensures
                - if (number_of_threads_alive() > 0 && the threads are currently supposed to be executing) then
                    - returns true
                - else
                    - returns false
        !*/

        unsigned long number_of_threads_alive (
        ) const;
        /*!
            ensures
                - returns the number of threads that are currently alive (i.e.
                  the number of threads that have started but not yet terminated)
        !*/

        unsigned long number_of_threads_registered (
        ) const;
        /*!
            ensures
                - returns the number of threads that have been registered by
                  calls to register_thread()
        !*/

        void wait (
        ) const;
        /*!
            requires
                - is not called from one of this object's threads 
            ensures
                - if (number_of_threads_alive() > 0) then
                    - blocks until all the threads in this object have terminated 
                      (i.e. blocks until number_of_threads_alive() == 0)
        !*/

        void start (
        );
        /*!
            ensures
                - #number_of_threads_alive() == number_of_threads_registered()
                - #is_running() == true
                - #should_stop() == false
                - all the threads registered are up and running. 
            throws
                - std::bad_alloc or dlib::thread_error
                    If either of these exceptions are thrown then 
                    #is_running() == false and should_stop() == true
        !*/

        void pause (
        );
        /*!
            ensures
                - #is_running() == false
        !*/

        void stop (
        );
        /*!
            ensures
                - #should_stop() == true
                - #is_running() == false
        !*/

    protected:

        template <
            typename T
            >
        void register_thread (
            T& object,
            void (T::*thread)()
        );
        /*!
            requires
                - (object.*thread)() forms a valid function call
                - the thread function does not throw
            ensures
                - registers the member function pointed to by thread as one of the threads
                  that runs when is_running() == true
                - #number_of_threads_registered() == number_of_threads_registered() + 1
                - if (is_running() == true)
                    - spawns this new member function in its own thread
                    - #number_of_threads_alive() += number_of_threads_alive() + 1
            throws
                - std::bad_alloc or dlib::thread_error
                    If either of these exceptions are thrown then 
                    #is_running() == false and should_stop() == true
        !*/

        bool should_stop (
        ) const;
        /*!
            requires
                - is only called from one of the registered threads in this object 
            ensures
                - if (is_running() == false && should_stop() == false) then
                    - blocks until (#is_running() == true || #should_stop() == true) 
                - if (this thread is supposed to terminate) then
                    - returns true
                - else
                    - returns false
        !*/

    private:

        // restricted functions
        multithreaded_object(multithreaded_object&);        // copy constructor
        multithreaded_object& operator=(multithreaded_object&);    // assignment operator
    };

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_MULTITHREADED_OBJECT_EXTENSIOn_ABSTRACT_