summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/threads/threaded_object_extension.h
blob: dcf00daeadb4a0280fcacc5152f98d82ba90c92a (plain)
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
// Copyright (C) 2007  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_THREADED_OBJECT_EXTENSIOn_
#define DLIB_THREADED_OBJECT_EXTENSIOn_ 

#include "threaded_object_extension_abstract.h"
#include "threads_kernel.h"
#include "auto_mutex_extension.h"
#include "../algs.h"
#include "../assert.h"

namespace dlib
{

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

    class threaded_object
    {
        /*!
            INITIAL VALUE
                - is_running_ == false
                - is_alive_ == false 
                - should_stop_ == false
                - should_respawn_ == false

#ifdef ENABLE_ASSERTS
                - id_valid == false 
                - id1 == get_main_thread_id()
#endif

            CONVENTION
                - is_running() == is_running_
                - is_alive() == is_alive_
                - should_stop() == should_stop_
                - should_respawn() == should_respawn_


#ifdef ENABLE_ASSERTS
                - if (when thread() is executing) then
                    - id1 == the id of the running thread 
                    - id_valid == true
                - else
                    - id1 == an undefined value
                    - id_valid == false
#endif

                - m_ == the mutex used to protect all our variables
                - s == the signaler for m_
        !*/

    public:

        threaded_object (
        );

        virtual ~threaded_object (
        );

        bool is_running (
        ) const;

        bool is_alive (
        ) const;

        void wait (
        ) const;

        void start (
        );

        void restart (
        );

        void set_respawn (
        );

        bool should_respawn (
        ) const;

        void pause (
        );

        void stop (
        );

    protected:

        bool should_stop (
        ) const;

    private:

        void thread_helper(
        );

        virtual void thread (
        ) = 0;

        mutex m_;
        signaler s;
        thread_id_type id1;
        bool is_running_;
        bool is_alive_;
        bool should_stop_;
        bool should_respawn_;
        bool id_valid;

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

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

}

#ifdef NO_MAKEFILE
#include "threaded_object_extension.cpp"
#endif

#endif // DLIB_THREADED_OBJECT_EXTENSIOn_