C++ library for asynchronous and event-driven execution
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mbandic 23fdd03dfe Refactor preprocessor directives, wait for reference and const objetct, multiple listeners, fix nameless timer functions, disable increase runners 6 months ago
.vscode Refactor preprocessor directives, wait for reference and const objetct, multiple listeners, fix nameless timer functions, disable increase runners 6 months ago
lib Refactor preprocessor directives, wait for reference and const objetct, multiple listeners, fix nameless timer functions, disable increase runners 6 months ago
test Refactor preprocessor directives, wait for reference and const objetct, multiple listeners, fix nameless timer functions, disable increase runners 6 months ago
.gitignore Timer functions, test, comments and readme 7 months ago
README.md Refactor preprocessor directives, wait for reference and const objetct, multiple listeners, fix nameless timer functions, disable increase runners 6 months ago

README.md

Asynco

A C++ library for event-driven asynchronous multi-threaded programming.

Features

  • Object oriented
  • Small and easy to integrate
  • Header only
  • Asynchronous programming
  • Multithread
  • Asynchronous timer functions: interval, timeout
  • Typed events (on, emit, off)
  • Event loops
  • Multiple parallel execution loops

Installation

Just download the latest release and unzip it into your project.

#define NUM_OF_RUNNERS 2            // To change the number of threads used by asynco

#include "asynco/lib/asynco.hpp"    // asynco(), wait()
#include "asynco/lib/event.hpp"     // event
#include "asynco/lib/rotor.hpp"     // interval, timeout
#include "asynco/lib/runner.hpp"    // for own loop
using namespace marcelb;

Usage

Time asynchronous functions

// start interval
interval inter1 ([]() {
     cout << "Interval 1" << endl;
}, 1000);

// stop interval
inter1.clear();

// start timeout
timeout time1 ( [] () {
    cout << "Timeout 1 " << endl;
}, 10000);

// stop timeout
time1.clear();

Make functions asynchronous

/**
* Run an lambda function asynchronously
*/

asynco( []() {
    sleep_for(2s);   // only for simulating long duration function
    cout << "asynco" << endl;
    return 5;
});


/**
 * Run not lambda function
*/

void notLambdaFunction() {
    cout << "Call to not lambda function" << endl;
}

asynco (notLambdaFunction);

/**
 * Run class method
*/

class clm {
    public:
    void classMethode() {
        cout << "Call class method" << endl;
    }
};

clm classes;
asynco( [&classes] () {
    classes.classMethode();
});



/**
* Wait after runned as async
*/

auto a = asynco( []() {
    sleep_for(2s);   // only for simulating long duration function
    cout << "asynco" << endl;
    return 5;
});

cout << wait(a) << endl;

/**
* Wait async function call and use i cout
*/

cout << wait(asynco( [] () {
    sleep_for(chrono::seconds(1)); // only for simulating long duration function
    cout << "wait end" << endl;
    return 4;
})) << endl;

/**
* Sleep with timeout sleep implement
*/

void sleep_to (int _time) {
    promise<void> _promise;
    timeout t( [&]() {
        _promise.set_value();
    }, _time);

    return _promise.get_future().get();
}

sleep_to(3000);

/**
* Catch promise reject
*/

void promise_reject (int _time) {
    promise<void> _promise;
    timeout t( [&]() {
        try {
            // simulate except
            throw runtime_error("Error simulation");
            _promise.set_value();
        } catch (...) {
            _promise.set_exception(current_exception());
        }
    }, _time);

    return _promise.get_future().get();
}

try {
    promise_reject(3000);
} catch (runtime_error err) {
    cout<< err.what() << endl;
}

Events

/**
* initialization of typed events
*/

event<int, int> ev2int;
event<int, string> evintString;
event<> evoid;

ev2int.on("sum", [](int a, int b) {
    cout << "Sum " << a+b << endl;
});

evintString.on("substract", [](int a, string b) {
    cout << "Substract " << a-stoi(b) << endl;
});

evoid.on("void", []() {
    cout << "Void emited" << endl;
});

// multiple listeners

string emited2 = "2";

evoid.on("void", [&]() {
    cout << "Void emited " << emited2 << endl;
});

sleep(1);

/**
* Emit
*/

ev2int.emit("sum", 5, 8);

sleep(1);
evintString.emit("substract", 3, to_string(2));

sleep(1);
evoid.emit("void");

// Turn off the event listener

evoid.off("void");
evoid.emit("void"); // nothing is happening

Extend own class whit events

class myOwnClass : public event<int> {
    public:
    myOwnClass() : event() {};
};

myOwnClass myclass;

timeout t( [&] {
    myclass.emit("constructed", 1);
}, 200);

myclass.on("constructed", [] (int i) {
    cout << "Constructed " << i  << endl;
});

License

APACHE 2.0

Support & Feedback

For support and any feedback, contact the address: marcelb96@yahoo.com.

Contributing

Contributions are always welcome!

Feel free to fork and start working with or without a later pull request. Or contact for suggest and request an option.