What is ev_async in libev for?

Asked 2 years ago, Updated 2 years ago, 114 views

I couldn't understand the document very well.
http://linux.die.net/man/3/ev

I would appreciate it if you could let me know if you know more about it.

linux socket

2022-09-29 22:04

1 Answers

For libev documents, you should refer to Head office README.

ev_async provides secure event notification for event loops in a multi-threaded environment.For example, for an event loop where one thread is ev_run(), another thread wants to notify you of the end of the loop.

Use ev_async to list the sample code for ending the event loop from another thread below.(※ Error handling is omitted.)

#include<stdio.h>
# include <unistd.h>
# include <pthread.h>
# include <ev.h>

structure ev_loop*loop;
structure ev_async shutdown_w;

static void*
thread_main(void*arg){
    sleep(3);
    fprintf(stderr, "ev_async_send\n");
    ev_async_send(loop, & shutdown_w); // thread safe
    return NULL;
}

static void
on_shutdown(struct ev_loop*loop, struct ev_async*w, int events){
    fprintf(stderr, "on_shutdown\n");
    ev_break(loop,EVBREAK_ALL);
}

int
main(intargc,char*argv[]){
    pthread_tid;

    loop = ev_default_loop(0);

    ev_async_init(&shutdown_w, on_shutdown);
    ev_async_start(loop, & shutdown_w);

    pthread_create(&id, NULL, thread_main, NULL);

    fprintf(stderr, "ev_run:start\n";
    ev_run(loop,0);
    fprintf(stderr, "ev_run:stop\n";

    return 0;
}


2022-09-29 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.