[1016] | 1 | // **********************************************************************
|
---|
| 2 | //
|
---|
| 3 | // Copyright (c) 2000
|
---|
| 4 | // Object Oriented Concepts, Inc.
|
---|
| 5 | // Billerica, MA, USA
|
---|
| 6 | //
|
---|
| 7 | // All Rights Reserved
|
---|
| 8 | //
|
---|
| 9 | // **********************************************************************
|
---|
| 10 |
|
---|
| 11 | #include <JTC/JTC.h>
|
---|
| 12 |
|
---|
| 13 | #include <time.h>
|
---|
| 14 |
|
---|
| 15 | #ifdef HAVE_STD_IOSTREAM
|
---|
| 16 | using namespace std;
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | //
|
---|
| 20 | // This class will display the time every 100 milliseconds until
|
---|
| 21 | // stopped.
|
---|
| 22 | //
|
---|
| 23 | class Clock : public JTCRunnable
|
---|
| 24 | {
|
---|
| 25 | JTCThreadHandle timer_; // The thread that this class runs in.
|
---|
| 26 |
|
---|
| 27 | public:
|
---|
| 28 |
|
---|
| 29 | //
|
---|
| 30 | // Constructor.
|
---|
| 31 | //
|
---|
| 32 | Clock()
|
---|
| 33 | {
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | //
|
---|
| 37 | // Display the current time.
|
---|
| 38 | //
|
---|
| 39 | void repaint()
|
---|
| 40 | {
|
---|
| 41 | time_t t = time(0);
|
---|
| 42 |
|
---|
| 43 | //
|
---|
| 44 | // The more observant will note that ctime_r should be used
|
---|
| 45 | // under a multi-threaded environment (UNIX). However, for
|
---|
| 46 | // this test ctime can safely be used since it is known that
|
---|
| 47 | // only this thread will call the function.
|
---|
| 48 | //
|
---|
| 49 | cout << ctime(&t) << endl;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | //
|
---|
| 53 | // Called when the thread is started.
|
---|
| 54 | //
|
---|
| 55 | void run()
|
---|
| 56 | {
|
---|
| 57 | //
|
---|
| 58 | // While the thread is non-zero continue.
|
---|
| 59 | //
|
---|
| 60 | while(timer_)
|
---|
| 61 | {
|
---|
| 62 | //
|
---|
| 63 | // Sleep for 100 milliseconds.
|
---|
| 64 | //
|
---|
| 65 | try
|
---|
| 66 | {
|
---|
| 67 | timer_ -> sleep(100);
|
---|
| 68 | }
|
---|
| 69 | catch(const JTCInterruptedException&)
|
---|
| 70 | {
|
---|
| 71 | }
|
---|
| 72 | //
|
---|
| 73 | // Repaint the time.
|
---|
| 74 | //
|
---|
| 75 | repaint();
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | //
|
---|
| 80 | // Start the thread.
|
---|
| 81 | //
|
---|
| 82 | void start()
|
---|
| 83 | {
|
---|
| 84 | if(!timer_)
|
---|
| 85 | {
|
---|
| 86 | //
|
---|
| 87 | // Create a new thread with this as the Runnable
|
---|
| 88 | // argument.
|
---|
| 89 | //
|
---|
| 90 | timer_ = new JTCThread(JTCRunnableHandle(this));
|
---|
| 91 | //
|
---|
| 92 | // Start the thread running.
|
---|
| 93 | //
|
---|
| 94 | timer_ -> start();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | //
|
---|
| 99 | // To stop the thread, mark the thread as invalid.
|
---|
| 100 | //
|
---|
| 101 | void stop()
|
---|
| 102 | {
|
---|
| 103 | timer_ = JTCThreadHandle();
|
---|
| 104 | }
|
---|
| 105 | };
|
---|
| 106 |
|
---|
| 107 | #ifndef HAVE_NO_EXPLICIT_TEMPLATES
|
---|
| 108 | template class JTCHandleT<Clock>;
|
---|
| 109 | #else
|
---|
| 110 | # ifdef HAVE_PRAGMA_DEFINE
|
---|
| 111 | # pragma define(JTCHandleT<Clock>)
|
---|
| 112 | # endif
|
---|
| 113 | #endif
|
---|
| 114 |
|
---|
| 115 | int
|
---|
| 116 | main(int argc, char** argv)
|
---|
| 117 | {
|
---|
| 118 | try
|
---|
| 119 | {
|
---|
| 120 | //
|
---|
| 121 | // A user of the JTC library must create an instance of this
|
---|
| 122 | // class to initialize the library.
|
---|
| 123 | //
|
---|
| 124 | JTCInitialize bootJTC(argc, argv);
|
---|
| 125 | //
|
---|
| 126 | // Create a new instance of the clock class.
|
---|
| 127 | //
|
---|
| 128 | JTCHandleT<Clock> c = new Clock();
|
---|
| 129 | //
|
---|
| 130 | // Start the clock thread running.
|
---|
| 131 | //
|
---|
| 132 | c -> start();
|
---|
| 133 | //
|
---|
| 134 | // Put the main thread to sleep for 5 seconds.
|
---|
| 135 | //
|
---|
| 136 | JTCThread::sleep(1000*5);
|
---|
| 137 | //
|
---|
| 138 | // Stop the clock thread.
|
---|
| 139 | //
|
---|
| 140 | c -> stop();
|
---|
| 141 | //
|
---|
| 142 | // The instance of the JTCInitialize class will ensure that
|
---|
| 143 | // this block is not exited until all threads terminate.
|
---|
| 144 | //
|
---|
| 145 | }
|
---|
| 146 | catch(const JTCException& e)
|
---|
| 147 | {
|
---|
| 148 | cerr << "JTCException: " << e.getMessage() << endl;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return 0;
|
---|
| 152 | }
|
---|