fast_signal now works with event_loop. Not too happy about the in_loop_wrapper implem...
[beacon-ss.git] / event_loop_test.cpp
blob6fb990b3a4be04a57f072d07c09f2d57bb116214
2 #ifdef HAVE_CONFIG_H
3 #include <config.h>
4 #endif
6 #include <iostream>
7 #include <beacon/event_loop.hpp>
8 #include <boost/bind.hpp>
9 #include <beacon/reference_countable.hpp>
11 using namespace std;
12 using namespace beacon;
14 unsigned times_called = 0;
15 class inv : public invokable {
16 public:
18 inv() : invokable(0) {
21 void invoke() {
22 times_called++;
25 bool operator==(invokable const & other) {
26 return this == &other;
30 void function() {
31 times_called++;
34 int main(int argc, char * argv[]) {
36 event_loop el;
38 el.start();
40 inv in;
42 //fast
43 enqueue<void()>::callable_type * fn = enqueue<void()>::call(el, &function, (void const *)(&function));
45 //slow - approx. 4x slower
46 //enqueue<void()>::invokable_type * fn = enqueue<void()>::invoke(el, &function, (void const *)(&function));
48 boost::xtime start, end, now;
49 boost::xtime_get(&start, boost::TIME_UTC);
51 for(int i = 0; i < 1000000; i++) {
52 (*fn)(); //this leaks when used with future pointer
53 //beacon::scoped_future<void> res((*fn)()); //this wraps the future pointer so that it doesn't leak
56 delete fn;
58 boost::xtime_get(&now, boost::TIME_UTC);
59 now.nsec += 1000;
60 boost::thread::sleep(now);
62 cout << "dequing ";
63 cout << el.dequeue((void const *)(&function)) << " calls" << endl;
65 el.join();
67 boost::xtime_get(&end, boost::TIME_UTC);
69 double diff = end.sec - start.sec + (end.nsec - start.nsec) / 1e9;
71 cout << "time: " << diff << std::endl;
72 cout << "times called: " << times_called << endl;
73 return EXIT_SUCCESS;