~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/src/video/test/test_thread.cpp

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "cc_thread.h"
2
 
#include <memory>
3
 
#include <iostream>
4
 
#include "noncopyable.h"
5
 
 
6
 
class CancellableBusyThread : public ost::Thread {
7
 
    public:
8
 
        CancellableBusyThread() : started_(true), x_(0)
9
 
        {}
10
 
 
11
 
        virtual void run()
12
 
        {
13
 
            x_ = new int(0);
14
 
            while (started_) {
15
 
                (*x_) += 1;
16
 
                yield();
17
 
            }
18
 
        }
19
 
 
20
 
        /* terminate() should always be called at the start of any
21
 
         * destructor of a class derived from Thread to assure the remaining
22
 
         * part of the destructor is called without the thread still executing.
23
 
         */
24
 
        virtual ~CancellableBusyThread()
25
 
        {
26
 
            std::cout << __PRETTY_FUNCTION__ << std::endl;
27
 
            ost::Thread::terminate();
28
 
            delete x_;
29
 
            x_ = 0;
30
 
        }
31
 
 
32
 
        bool started_;
33
 
    private:
34
 
        int *x_;
35
 
        NON_COPYABLE(CancellableBusyThread);
36
 
};
37
 
 
38
 
class EventThread : public ost::Thread {
39
 
    public:
40
 
        EventThread() : ost::Thread(), x_(0), event_()
41
 
        {}
42
 
 
43
 
        virtual void run()
44
 
        {
45
 
            event_.signal();
46
 
        }
47
 
 
48
 
        // called from other threads
49
 
        void waitForEvent()
50
 
        {
51
 
            event_.wait();
52
 
        }
53
 
 
54
 
        /* terminate() should always be called at the start of any
55
 
         * destructor of a class derived from Thread to assure the remaining
56
 
         * part of the destructor is called without the thread still executing.
57
 
         */
58
 
        virtual ~EventThread()
59
 
        {
60
 
            ost::Thread::terminate();
61
 
            std::cout << __PRETTY_FUNCTION__ << std::endl;
62
 
        }
63
 
    private:
64
 
        int x_;
65
 
        ost::Event event_;
66
 
};
67
 
 
68
 
int main()
69
 
{
70
 
    EventThread *th = new EventThread;
71
 
    th->start();
72
 
    th->waitForEvent();
73
 
    std::cout << "event has happened..." << std::endl;
74
 
    th->join();
75
 
    delete th;
76
 
 
77
 
    std::auto_ptr<CancellableBusyThread> busy(new CancellableBusyThread);
78
 
    std::cout << "Starting busy thread" << std::endl;
79
 
    busy->start();
80
 
    busy->started_ = false;
81
 
    busy.reset();
82
 
    std::cout << "Finished busy thread" << std::endl;
83
 
 
84
 
    return 0;
85
 
}