summaryrefslogtreecommitdiffstats
path: root/src/ml/dlib/examples/multithreaded_object_ex.cpp
blob: fed32a91c9b93d04d6e9bb9f2e98920ba9f1d7a0 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the multithreaded_object.

    This is a very simple example.  It creates 3 threads that
    just print messages to the screen.



    Example program output:
    0 INFO  [1] mto: thread1(): hurray threads!
    0 INFO  [2] mto: thread2(): hurray threads!
    0 INFO  [3] mto: thread2(): hurray threads!
  700 INFO  [1] mto: thread1(): hurray threads!
  800 INFO  [2] mto: thread2(): hurray threads!
  801 INFO  [3] mto: thread2(): hurray threads!
 1400 INFO  [1] mto: thread1(): hurray threads!
 1604 INFO  [2] mto: thread2(): hurray threads!
 1605 INFO  [3] mto: thread2(): hurray threads!
 2100 INFO  [1] mto: thread1(): hurray threads!
 2409 INFO  [2] mto: thread2(): hurray threads!
 2409 INFO  [3] mto: thread2(): hurray threads!
 2801 INFO  [1] mto: thread1(): hurray threads!
 3001 INFO  [0] mto: paused threads
 6001 INFO  [0] mto: starting threads back up from paused state
 6001 INFO  [2] mto: thread2(): hurray threads!
 6001 INFO  [1] mto: thread1(): hurray threads!
 6001 INFO  [3] mto: thread2(): hurray threads!
 6705 INFO  [1] mto: thread1(): hurray threads!
 6805 INFO  [2] mto: thread2(): hurray threads!
 6805 INFO  [3] mto: thread2(): hurray threads!
 7405 INFO  [1] mto: thread1(): hurray threads!
 7609 INFO  [2] mto: thread2(): hurray threads!
 7609 INFO  [3] mto: thread2(): hurray threads!
 8105 INFO  [1] mto: thread1(): hurray threads!
 8413 INFO  [2] mto: thread2(): hurray threads!
 8413 INFO  [3] mto: thread2(): hurray threads!
 8805 INFO  [1] mto: thread1(): hurray threads!

  The first column is the number of milliseconds since program start, the second
  column is the logging level, the third column is the thread id, and the rest
  is the log message.
*/


#include <iostream>
#include <dlib/threads.h>
#include <dlib/misc_api.h>  // for dlib::sleep
#include <dlib/logger.h>

using namespace std;
using namespace dlib;

logger dlog("mto");

class my_object : public multithreaded_object
{
public:
    my_object()
    {
        // register which functions we want to run as threads.  We want one thread running
        // thread1() and two threads to run thread2().  So we will have a total of 3 threads
        // running.
        register_thread(*this,&my_object::thread1);
        register_thread(*this,&my_object::thread2);
        register_thread(*this,&my_object::thread2);

        // start all our registered threads going by calling the start() function
        start();
    }

    ~my_object()
    {
        // Tell the thread() function to stop.  This will cause should_stop() to 
        // return true so the thread knows what to do.
        stop();

        // Wait for the threads to stop before letting this object destruct itself.
        // Also note, you are *required* to wait for the threads to end before 
        // letting this object destruct itself.
        wait();
    }

private:

    void thread1()
    {
        // This is a thread.  It will loop until it is told that it should terminate.
        while (should_stop() == false)
        {
            dlog << LINFO << "thread1(): hurray threads!";
            dlib::sleep(700);
        }
    }

    void thread2()
    {
        // This is a thread.  It will loop until it is told that it should terminate.
        while (should_stop() == false)
        {
            dlog << LINFO << "thread2(): hurray threads!";
            dlib::sleep(800);
        }
    }

};

int main()
{
    // tell the logger to output all messages
    dlog.set_level(LALL);

    // Create an instance of our multi-threaded object.   
    my_object t;

    dlib::sleep(3000);
    
    // Tell the multi-threaded object to pause its threads.  This causes the
    // threads to block on their next calls to should_stop().
    t.pause();
    dlog << LINFO << "paused threads";

    dlib::sleep(3000);
    dlog << LINFO << "starting threads back up from paused state";

    // Tell the threads to unpause themselves.  This causes should_stop() to unblock 
    // and to let the threads continue.
    t.start();

    dlib::sleep(3000);

    // Let the program end.  When t is destructed it will gracefully terminate your
    // threads because we have set the destructor up to do so.
}