a demo code considering if ev_async event missing

debuguo wateraworld at gmail.com
Tue Jul 3 18:01:56 CEST 2012

Hello everyone,
I wrote a demo code, to find out if there was ev_async event missing
when ev_async_send called in diffrent thread.
I add 2 counter, one in the function which would call ev_async_send,
the other in async_callback. a timer was used and assigned to 0.001s,
0.0001s, 0.00001s. When the timer expired, timeout_callback would be
called, then the ev_async_send told the other one something happend,
then the async_callback executed in the other thread. I'm afraid
something really happened, but the async_callback would not be called.
If my test code was not totally wrong and the usage of libev was
right, then the result means:
the more frequently ev_async_send was called and the longer ev_loop
running, the farther two counter value differ.
Tell me directly if any mistake in the demo code, Thank you.

#include <ev.h>
#include <stdio.h> // for puts
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t lock;
double timeout = 0.00001;
ev_timer timeout_watcher;
int timeout_count = 0;

ev_async async_watcher;
int async_count = 0;

struct ev_loop* loop2;

void* loop2thread(void* args)
    ev_run(loop2, 0);
    return NULL;

static void async_cb (EV_P_ ev_async *w, int revents)
    //puts ("async ready");
    printf("async = %d, timeout = %d \n", async_count, timeout_count);

static void timeout_cb (EV_P_ ev_timer *w, int revents)
    //puts ("timeout");
    ev_async_send(loop2, &async_watcher);

    w->repeat = timeout;
    ev_timer_again(loop, &timeout_watcher);


int main (int argc, char** argv)
    if (argc < 2) {
        puts("./demo timeout");
        return -1;
    timeout = atof(argv[1]);

    pthread_mutex_init(&lock, NULL);
    pthread_t thread;
    loop2 = ev_loop_new(0);

    ev_async_init(&async_watcher, async_cb);
    ev_async_start(loop2, &async_watcher);
    pthread_create(&thread, NULL, loop2thread, NULL);

    struct ev_loop *loop = EV_DEFAULT;
    ev_timer_init (&timeout_watcher, timeout_cb, timeout, 0.);
    ev_timer_start (loop, &timeout_watcher);

    ev_run (loop, 0);
    pthread_join(thread, NULL);

    return 0;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: a.c
Type: text/x-csrc
Size: 1485 bytes
Desc: not available
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20120704/8b5fb642/attachment.c>

More information about the libev mailing list