Debian Codegraph @

protolib/examples/threadExample.cpp

raw
1 #include "protokit.h"
2
3 #ifdef UNIX
4 #include <unistd.h> // for sleep() function
5 #endif // UNIX
6
7 /**
8 * @class Ticker
9 *
10 * @brief This example illustrates the use of threaded ProtoDispatcher operation
11 *
12 * A "Ticker" is started in separate thread that has a timer that fires off
13 * once per second while the main thread goes about its business (sleeping
14 * in 10 second chunks). The Ticker::Stop() method calls the
15 * ProtoDispatcher::Stop() method which destroys the dispatcher thread
16 * Alternatively, the Ticker::timer could have been Deactivated without
17 * touch the dispatcher thread, but a new dispatcher thread can be
18 * instantiated by calling ProtoDispatcher::StartThread() ... so there
19 * is flexibility here.
20 *
21 * When using a threaded ProtoDispatcher, make sure to call SuspendThread()
22 * before manipulating dispatcher objects. (See Ticker::StartTimer() and
23 * Ticker::StopTimer() as an example)
24 */
25 class Ticker
26 {
27 public:
28 Ticker();
29 ~Ticker();
30
31 bool Start();
32 void Stop();
33
34 bool StartTimer()
35 {
36 bool result = dispatcher.SuspendThread();
37 if (result && !timer.IsActive())
38 dispatcher.ActivateTimer(timer);
39 dispatcher.ResumeThread();
40 return result;
41 }
42 bool StopTimer()
43 {
44 bool result = dispatcher.SuspendThread();
45 if (result && timer.IsActive())
46 timer.Deactivate();
47 dispatcher.ResumeThread();
48 return result;
49 }
50
51 private:
52 bool OnTimeout(ProtoTimer& theTimer);
53 ProtoDispatcher dispatcher;
54 ProtoTimer timer;
55 }; // end class Ticker
56
57 Ticker::Ticker()
58 {
59 timer.SetListener(this, &Ticker::OnTimeout);
60 timer.SetInterval(1.0);
61 timer.SetRepeat(-1);
62 }
63
64 Ticker::~Ticker()
65 {
66 Stop();
67 }
68
69 bool Ticker::Start()
70 {
71 //dispatcher.ActivateTimer(timer);
72 if (dispatcher.StartThread())
73 {
74 return true;
75 }
76 else
77 {
78 PLOG(PL_ERROR, "threadExample: Ticker::Start() thread start failure ...\n");
79 timer.Deactivate();
80 return false;
81 }
82 } // end Ticker::Start()
83
84 void Ticker::Stop()
85 {
86 dispatcher.Stop();
87 if (timer.IsActive()) timer.Deactivate();
88 } // end Ticker::Stop()
89
90 /**
91 * IMPORTANT NOTE @note Remember this timeout handler
92 * is being called by the dispatcher child thread, _not_
93 * the main thread since we're using ProtoDispatcher::StartThread()
94 * to run the dispatcher
95 */
96 bool Ticker::OnTimeout(ProtoTimer& theTimer)
97 {
98 TRACE("threadExample: child thread Ticker::OnTimeout() ...\n");
99 return true;
100 } // end Ticker::OnTimeout()
101
102 int main(int argc, char* argv[])
103 {
104 Ticker ticker;
105
106 // This "starts" the ticker thread although it just sits and
107 // waits until its "StartTimer()" method is called
108 if (!ticker.Start())
109 {
110 PLOG(PL_ERROR, "threadExample: Ticker::Start() error\n");
111 return -1;
112 }
113
114 int count = 2;
115 while (count--)
116 {
117 #ifdef WIN32
118 Sleep(3000);
119 #else
120 sleep(3);
121 #endif // if/else WIN32
122 if (count)
123 {
124 TRACE("threadExample: main thread 3 seconds have passed (starting ticker timer)\n");
125 ticker.StartTimer();
126 }
127 else
128 {
129 TRACE("threadExample: main thread 3 more seconds have passed (stopping ticker timer)\n");
130 ticker.Stop();
131 }
132
133 }
134 TRACE("threadExample: main thread ticker stopped.\n");
135 TRACE("threadExample: main thread Done.\n");
136 return 0;
137
138 } // end main()