[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] pthread adds a socket to libevent but exits - undefined behavior ?



On Wed, Apr 10, 2019 at 11:23 AM Ani A <aniruddha.a@xxxxxxxxx> wrote:
>
> Hello,

Hi!

> I am trying to understand some legacy code, which is mostly single
> thread libevent based, there are some cases where pthreads are spawned
> and die out instantly. The scenario is
>
> pthread_create
>   --> thread_callback
>     --> In thread callback, create fd; event_new( ..cbk, &fd) -> uses
> evbase of main
>     <thread ends>
>  cbk() gets called and fd is used.
>
> But is this correct ? or just works because of undefined behavior ?

This is totally correct (although this is not a very good design
decision, but since you said that this is legacy, let's omit this).

> The thread need not be present when the event callback is invoked ?

What for? The callback will be always called from the event loop, it
will not be called from the place where event_new()/event_add() was
called for it (and this is another reason for considering this as a
bad design, since there is no profit in making thread to just adding
events, unless that thread doing something else).

> (since the fd was created in thread context)

Threads shares the fd table with the main progress (or we can call
this main thread, it depends on OS though), IOW everything should be
fine.

> P.S. libevent is initialised with evthread_use_pthreads()  and linked
> with libevent_pthreads

Good to know, so this should means that everything should work.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.