moved the token invalidation from ~trackable to signal_base::do_disconnect where...
[beacon-ss.git] / beacon / event_loop.hpp
blob7a348e85c368f3800e55c25a7ad3f85d1603e969
1 /**
2 * beacon
3 * Author: Lukas Krejci <krejci.l@centrum.cz>, (C) 2008
4 * Copyright: See COPYING file that comes with this distribution
5 */
7 #ifndef BEACON_EVENT_LOOP_H
8 #define BEACON_EVENT_LOOP_H
10 #include <beacon/event_queue.hpp>
12 #include <boost/thread.hpp>
13 #include <boost/bind.hpp>
15 namespace beacon {
17 /**
18 * Event loop is a wrapper of event_queue that implements
19 * a simple event queue. I.e. it starts a thread in which
20 * all the events are invoked.
22 class event_loop : public event_queue {
23 public:
25 /**
26 * A new thread is started to handle this event loop.
28 event_loop() :
29 _thread(0),
30 _running(false),
31 _finished(false)
34 /**
35 * This call does NOT block until the underlying thread finishes.
36 * It just destroys the event_loop object without stopping
37 * the underlying thread. Be sure to call @link join method before
38 * the event_loop is destroyed.
40 ~event_loop() {
41 if (_thread) delete _thread;
44 /**
45 * Starts the event loop. The events can be enqueued and dequeued before
46 * the event loop is started but they won't be processed until it is.
48 * This method has no effect if there already is a thread running and
49 * IS NOT thread-safe.
51 void start() {
52 if (_thread == 0) {
53 _running = true;
54 _finished = false;
55 _thread = new boost::thread(boost::bind(&event_loop::run, this));
59 /**
60 * Joins the event loop thread. If the thread is started using the {@link start} method
61 * while another thread is executing this method, the result is undefined.
63 * This method IS NOT thread-safe.
65 void join();
66 private:
68 //the thread we're executing the events in
69 boost::thread * _thread;
71 //controlling the thread alive-state
72 bool _running;
73 bool _finished;
75 //main loop
76 void run();
79 } //namespage
81 #endif