Chaitanya
Chaitanya

Reputation: 3479

How to create asynchronous method in C++ using libev

I am trying to create an asynchronous method in C++ using libev. If needed I can pass a callback method as an argument.

For instance

test();
printf("After test() method\n");

the test() is an asynchronous method, so the next printf() statement should be executed before test() completes its execution.

I tried using libev for this simple example :

void testCallback(struct ev_loop *loop, struct ev_io  *watcher, int revents)
{
    sleep(5);
    ev_io_stop(loop, watcher);
}

int test()
{
    struct ev_loop *loop = ev_default_loop(0);
    ev_io watch;

    ev_io_init(&watch, testCallback, 0, EV_READ);
    ev_io_start(loop, &watch);

    ev_run(loop, 0);

    return 0;
}

int main() {
    test();
    printf("After test() method");
    return 0;
}

In this example, the printf is getting executed after that event loop has stopped. Is this kind of functionality possible using libev ? I googled but couldn't get no example with this kind of need.

Upvotes: 0

Views: 2036

Answers (1)

Adnan Akbar
Adnan Akbar

Reputation: 718

From the code printf should be executed after the loop has stoped. Test is not asynchronus rather the testCallback is asynchronus. You might have misunderstood the logic.

Upvotes: 1

Related Questions