Fix multiple engine init problem and enable anonim init timers

multiple_init_problem
marcelb 2 months ago
parent 1ccac9dbf8
commit 9e773f55c9
  1. 22
      README.md
  2. 47
      lib/asynco.hpp
  3. 71
      lib/engine.hpp
  4. 14
      lib/timers.hpp
  5. 6
      lib/trigger.hpp
  6. 7
      src/engine.cpp
  7. 36
      src/timers.cpp
  8. 1
      test/compile.sh
  9. 67
      test/test.cpp

@ -77,6 +77,28 @@ int t = time1.expired();
// is it stopped
bool stoped = time1.stoped();
// If you don't want to save in a variable, but you want to start a timer, use these functions
// And you can also save them, they are only of the shared pointer type
auto d = Delayed( [](){
cout << "Delayed" << endl;
}, 2000);
auto p = Periodic( [](){
cout << "Periodic" << endl;
}, 700);
Periodic( [&] (){
cout << "Delayed expire " << d->expired() << endl;
cout << "Periodic ticks " << p->ticks() << endl;
cout << "Delayed stoped " << d->stoped() << endl;
cout << "Periodic stoped " << p->stoped() << endl;
}, 1000);
Delayed( [&](){
p->stop();
}, 10000);
```
Make functions asynchronous

@ -1,7 +1,7 @@
#ifndef _ASYNCO_
#define _ASYNCO_
#include <boost/asio.hpp>
#include "engine.hpp"
#include <iostream>
using namespace std;
@ -9,51 +9,6 @@ using namespace std;
namespace marcelb {
namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4
/**
* Internal anonymous class for initializing the ASIO context and thread pool
* !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
*/
class {
public:
boost::asio::io_context io_context;
void run() {
for (auto& runner : runners) {
runner.join();
}
}
private:
unique_ptr<boost::asio::io_service::work> work { [&] () {
return new boost::asio::io_service::work(io_context);
} ()};
vector<thread> runners { [&] () {
vector<thread> _runs;
unsigned int num_of_runners;
#ifdef NUM_OF_RUNNERS
num_of_runners = NUM_OF_RUNNERS;
#else
num_of_runners = thread::hardware_concurrency();
if (num_of_runners < HW_CONCURRENCY_MINIMAL) {
num_of_runners = HW_CONCURRENCY_MINIMAL;
}
#endif
for (int i=0; i<num_of_runners; i++) {
_runs.push_back(thread ( [this] () {
io_context.run();
}));
}
return _runs;
} ()};
} _asynco_engine;
/**
* Run the function asynchronously
*/

@ -0,0 +1,71 @@
#ifndef _ASYNCO_ENGINE_
#define _ASYNCO_ENGINE_
#include <vector>
#include <memory>
using namespace std;
#include <boost/asio.hpp>
namespace marcelb {
namespace asynco {
#define HW_CONCURRENCY_MINIMAL 4
/**
* Internal anonymous class for initializing the ASIO context and thread pool
* !!! It is anonymous to protect against use in the initialization of other objects of the same type !!!
*/
class Engine {
public:
boost::asio::io_context io_context;
void run() {
for (auto& runner : runners) {
runner.join();
}
}
private:
unique_ptr<boost::asio::io_service::work> work { [&] () {
return new boost::asio::io_service::work(io_context);
} ()};
vector<thread> runners { [&] () {
vector<thread> _runs;
unsigned int num_of_runners;
#ifdef NUM_OF_RUNNERS
num_of_runners = NUM_OF_RUNNERS;
#else
num_of_runners = thread::hardware_concurrency();
if (num_of_runners < HW_CONCURRENCY_MINIMAL) {
num_of_runners = HW_CONCURRENCY_MINIMAL;
}
#endif
for (int i=0; i<num_of_runners; i++) {
_runs.push_back(thread ( [this] () {
io_context.run();
}));
}
return _runs;
} ()};
};
extern Engine _asynco_engine;
}
}
#endif

@ -1,12 +1,10 @@
#ifndef _TIMERS_
#define _TIMERS_
#ifndef _ASYNCO_TIMERS_
#define _ASYNCO_TIMERS_
#include "asynco.hpp"
#include <chrono>
using namespace std;
using namespace marcelb;
using namespace asynco;
#include "asynco.hpp"
namespace marcelb {
namespace asynco {
@ -153,6 +151,10 @@ class delayed {
};
shared_ptr<periodic> Periodic(function<void()> callback, uint64_t time);
shared_ptr<delayed> Delayed(function<void()> callback, uint64_t time);
}
}

@ -1,5 +1,5 @@
#ifndef _TRIGGER_
#define _TRIGGER_
#ifndef _ASYNCO_TRIGGER_
#define _ASYNCO_TRIGGER_
#include <map>
#include <vector>
@ -8,7 +8,7 @@
using namespace std;
#include "asynco.hpp"
#include "engine.hpp"
namespace marcelb {
namespace asynco {
namespace triggers {

@ -0,0 +1,7 @@
#include "../lib/engine.hpp"
namespace marcelb::asynco {
Engine _asynco_engine;
};

@ -106,4 +106,40 @@ delayed::~delayed() {
stop();
}
mutex p_io, d_io;
vector<shared_ptr<periodic>> periodic_calls_container;
vector<shared_ptr<delayed>> delayed_calls_container;
shared_ptr<periodic> Periodic(function<void()> callback, uint64_t time) {
shared_ptr<periodic> periodic_ptr(make_shared<periodic>(callback, time));
async_ ( [&, periodic_ptr](){
lock_guard<mutex> lock(p_io);
periodic_calls_container.push_back(periodic_ptr);
for (uint32_t i=0; i<periodic_calls_container.size(); i++) {
if (periodic_calls_container[i]->stoped()) {
periodic_calls_container.erase(periodic_calls_container.begin()+i);
i--;
}
}
});
return periodic_ptr;
}
shared_ptr<delayed> Delayed(function<void()> callback, uint64_t time) {
shared_ptr<delayed> delayed_ptr(make_shared<delayed>(callback, time));
async_ ( [&, delayed_ptr](){
lock_guard<mutex> lock(p_io);
delayed_calls_container.push_back(delayed_ptr);
for (uint32_t i=0; i<delayed_calls_container.size(); i++) {
if (delayed_calls_container[i]->stoped() || delayed_calls_container[i]->expired()) {
delayed_calls_container.erase(delayed_calls_container.begin()+i);
i--;
}
}
});
return delayed_ptr;
}
};

@ -0,0 +1 @@
g++ test.cpp ../src/* -o test

@ -69,9 +69,9 @@ int main () {
// --------------- TIME ASYNCHRONOUS FUNCTIONS --------------
// /**
// * Init periodic and delayed; clear periodic and delayed
// */
/**
* Init periodic and delayed; clear periodic and delayed
*/
// periodic inter1 ([&]() {
// cout << "periodic prvi " << rtime_ms() - start << endl;
@ -129,11 +129,30 @@ int main () {
// cout << "nije isteko " << endl;
// }
// // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// auto d = Delayed( [](){
// cout << "Delayed" << endl;
// }, 2000);
// /**
// * Run an function asyncronic
// */
// auto p = Periodic( [](){
// cout << "Periodic" << endl;
// }, 700);
// Periodic( [&] (){
// cout << "Delayed expire " << d->expired() << endl;
// cout << "Periodic ticks " << p->ticks() << endl;
// cout << "Delayed stoped " << d->stoped() << endl;
// cout << "Periodic stoped " << p->stoped() << endl;
// }, 1000);
// Delayed( [&](){
// p->stop();
// }, 10000);
// // // // ------------------------ MAKE FUNCTIONS ASYNCHRONOUS -------------------------
// // /**
// // * Run an function asyncronic
// // */
// async_ ( []() {
// sleep_for(2s); // only for simulate log duration function
@ -155,13 +174,13 @@ int main () {
// );
// async(launch::async, [] () {
// cout << "Another thread in async style!" << endl;
// });
// // async(launch::async, [] () {
// // cout << "Another thread in async style!" << endl;
// // });
// /**
// * Call class method
// */
// // /**
// // * Call class method
// // */
// clm classes;
// async_ ( [&classes] () {
@ -170,17 +189,17 @@ int main () {
// sleep(5);
// /**
// * await_ after runned as async
// */
// // /**
// // * await_ after runned as async
// // */
// auto a = async_ ( []() {
// auto aa = async_ ( []() {
// sleep_for(2s); // only for simulate log duration function
// cout << "async_ 2" << endl;
// return 5;
// });
// cout << await_(a) << endl;
// cout << await_(aa) << endl;
// cout << "print after async_ 2" << endl;
// /**
@ -271,17 +290,19 @@ int main () {
// );
// }
// auto await_all = [&] () {
// auto await_all2 = [&] () {
// for (int i=0; i<fut_vec.size(); i++) {
// await_ (fut_vec[i]);
// }
// };
// --------------- EVENTS -------------------
// await_all2();
/**
* initialization of typed events
*/
// // --------------- EVENTS -------------------
// /**
// * initialization of typed events
// */
// trigger<int, int> ev2int;
// trigger<int, string> evintString;

Loading…
Cancel
Save