ev_async delays thread when joining

Juan Pablo L jpablolorenzetti at gmail.com
Sun Jan 2 01:10:30 CET 2011


Hi, i m new to libev and i m trying to use ev_async to signal some threads
for some events, everything is ok but when the threads end
they take very long to be joined, like in minutes which makes the
application not responsive (i need them to be joinable in the real
application), i m sure the problem is with ev_async,
i have tested without it and with it, the problem is the ev_async but i have
no idea where the problem is, doing strace it shows that the loop gets stack
at epoll in a futex call,
so i have put a test case together:

#include <stdlib.h>
#include <pthread.h>
#define EV_STANDALONE 1
#include <ev.c>


void *the_thread_function(struct ev_loop *evt_loop)
{
        ev_run(evt_loop,0);
        pthread_exit(NULL);
}

void the_cb(struct ev_loop *evt_loop,ev_async *as,int revents)
{
        printf("async_func\n");
}

int main()
{
        pthread_t thread;
        pthread_attr_t thread_attr;
        struct ev_loop *evt_loop;
        ev_async as_evt;
        char *message = (char*)malloc(10);
        int i = 2;

        // create the loop
        evt_loop = ev_loop_new(EVFLAG_AUTO);

        // create and start the watcher
        ev_async_init(&as_evt,(void*)the_cb);
        ev_async_start(evt_loop,&as_evt);


        // create the thread
        pthread_attr_init(&thread_attr);
        pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_JOINABLE);

pthread_create(&thread,&thread_attr,(void*)the_thread_function,(void*)evt_loop);
        pthread_attr_destroy(&thread_attr);

        while(--i >= 0)
        {
                printf("i = %d\n",i);
                ev_async_send(evt_loop,&as_evt);
                sleep(1);
        }

        ev_break(evt_loop,EVBREAK_ALL);

        printf("stopping watcher\n");
        ev_async_stop(evt_loop,&as_evt);
        printf("watcher stopped\n");

        printf("joining the thread\n");
        pthread_join(thread,NULL);
        printf("thread joined\n");
        printf("destroying loop\n");
        ev_loop_destroy(evt_loop);
        printf("loop destroyed\n");

        return 0;
}

Any comments will be helpful. Thank you very much!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20110101/a173f875/attachment.html>


More information about the libev mailing list