Switch atask, events, timers to asio

dev-asio-engine
marcelb 6 months ago
parent 35ff6bef4b
commit 14663d631a
  1. 129
      test/asio.cpp
  2. 76
      test/test.cpp

@ -1,129 +0,0 @@
#include <iostream>
#include <vector>
#include <thread>
#include <functional>
#include <boost/asio.hpp>
using namespace std;
int64_t rtime_ms() {
return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now()
.time_since_epoch())
.count();
}
// globals
int64_t start = rtime_ms();;
// timer global
boost::asio::io_context io_context;
unique_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(io_context));
class timer {
boost::asio::steady_timer st;
bool stop = false;
bool repeate;
function<void()> callback;
uint64_t time;
void init() {
st.async_wait( [this] (const boost::system::error_code&) {
if (!stop) {
callback();
if (repeate) {
reinit();
}
}
});
}
void reinit() {
st = boost::asio::steady_timer(io_context, boost::asio::chrono::milliseconds(time));
init();
}
public:
timer (function<void()> _callback, uint64_t _time, bool _repeate)
: st(io_context, boost::asio::chrono::milliseconds(_time)),
stop(false),
repeate(_repeate),
callback(_callback),
time(_time) {
init();
}
void clear() {
stop = true;
st.cancel();
}
~timer() {
clear();
}
};
vector<shared_ptr<timer>> timer_container;
timer setInterval(int interval, std::function<void()> func) {
cout << "set interval construction call" << endl;
// return make_shared<timer> (func, interval, true);
return timer (func, interval, true);
};
class interval {
shared_ptr<timer> _timer;
public:
interval(function<void()> callback, uint64_t time) : _timer(make_shared<timer> (callback, time, true)) {
timer_container.push_back(_timer);
}
};
timer setTimeout(int interval, std::function<void()> func) {
cout << "set timeout construction call" << endl;
// return make_shared<timer> (func, interval, false);
return timer(func, interval, false);
};
// void clearTimer(shared_ptr<timer> _timer) {
// _timer->stop = true;
// _timer->st.cancel();
// }
int main() {
vector<thread> loops;
for (int i =0; i<8; i++)
loops.push_back( thread([&]() {
while (true) {io_context.run();}
}));
timer timer1 = setInterval(1000, []() {
// cout << "Interval end " << rtime_ms() - start << endl;
cout << "Interval 1 " << this_thread::get_id() << endl;
});
interval([]() {
// cout << "Interval end " << rtime_ms() - start << endl;
cout << "Interval 2 " << this_thread::get_id() << endl;
}, 1500);
// timer timer2 = setTimeout(5000, [&]() {
// cout << "Timeout end " << rtime_ms() - start << endl;
// timer1.clear();
// });
for (int i =0; i<4; i++)
loops[i].join();
return 0;
}

@ -1,12 +1,14 @@
#define NUM_OF_RUNNERS 2
// #define NUM_OF_RUNNERS 2
#include "../lib/asynco.hpp"
#include "../lib/event.hpp"
#include "../lib/rotor.hpp"
// #include "../lib/rotor.hpp"
#include "../lib/filesystem.hpp"
#include "../lib/timers.hpp"
#include <iostream>
#include <unistd.h>
#include <thread>
using namespace std;
using namespace marcelb::asynco;
@ -67,26 +69,15 @@ int main () {
* Init interval and timeout; clear interval and timeout
*/
// interval( [&] () {
// cout << "interval 1: " << rtime_ms() - start << endl;
// }, 50);
// interval( [&] () {
// cout << "interval 1: " << rtime_ms() - start << endl;
// }, 100);
// interval( [&] () {
// cout << "interval 2: " << rtime_ms() - start << endl;
// }, 200);
// interval( [&] () {
// cout << "interval 3: " << rtime_ms() - start << endl;
// }, 300);
// vector<interval> intervals;
// for(int i=0; i<1000; i++) {
// intervals.push_back(interval( [i, &start]() {
// cout << "interval " << i << " end: " << rtime_ms() - start << endl;
// }, (i%5 +1)*1000));
// }
// interval( [&] () {
// cout << "interval 4: " << rtime_ms() - start << endl;
// }, 400);
// interval inter1 ([&]() {
// cout << "interval prvi " << rtime_ms() - start << endl;
@ -114,19 +105,24 @@ int main () {
// timeout time1 ( [&] () {
// cout << "Close interval 1 i 2 " << rtime_ms() - start << endl;
// // inter1.clear();
// // cout << "inter1.stop " << inter1.stop << endl;
// // inter2.clear();
// // cout << "inter2.stop " << inter2.stop << endl;
// }, 5000);
// // inter1.stop();
// cout << "inter1.stop " << endl;
// inter2.stop();
// cout << "inter2.stop " << endl;
// }, 8000);
// timeout time2 ([&] () {
// cout << "Close interval 3 " << rtime_ms() - start << endl;
// // inter3.clear();
// time1.clear();
// // cout << "Close interval 3 " << rtime_ms() - start << endl;
// inter3.stop();
// // time1.stop();
// }, 2000);
// interval ( [] () {
// cout << "BROJ TAJMERA " << _intern_asynco_timer_globals.timers.size() << endl;
// }, 5000);
// // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// /**
@ -293,6 +289,32 @@ int main () {
// cout << err.what() << endl;
// }
string data_;
auto start_read = rtime_us();
// fs::read("test1.txt", [&data_, &start_read] (string data, exception* error) {
// if (error) {
// cout << "Error " << error->what() << endl;
// } else {
// // cout << "Data " << endl << data << endl;
// // data_ = data;
// // cout << "Data_" << data_ << endl;
// cout << "read " << rtime_us() - start_read << endl;
// }
// });
fs::read2("test1.txt", [&data_, &start_read] (string data, exception* error) {
if (error) {
cout << "Error " << error->what() << endl;
} else {
// cout << "Data " << endl << data << endl;
// data_ = data;
// cout << "Data_" << data_ << endl;
cout << "read " << rtime_us() - start_read << endl;
}
});
cout << "Sleep" << endl;
sleep(100000); // only for testing

Loading…
Cancel
Save