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

[Libevent-users] Multithreading and breaking out of the event loop



Hi,

I have a C++ program interacting with a C static library that is
internally using libevent.

I am able to break out of the C lib's libevent main loop but the exact
order in which threads are executing worries me.

The full details and question can be found here:
https://stackoverflow.com/questions/54589066/multithreading-in-libevent

Regards,
Theodor

For ease, I will copy and paste the StackOverflow question here as well:
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------

I have one thread (Thread A) in a C static library that's creating the
libevent event_base after doing this:

#ifdef WIN32
    evthread_use_windows_threads();
#else
    evthread_use_pthreads();
#endif

This C static library is all single threaded and it's doing all the
libevent work itself: adding events, dispatching, main loop, etc, all
these being done from Thread A.

Now I have a C++ program that I link with the library above, calling
the entrance function into the C library from a boost::thread called
Thread B.

At some point into my C++ program, I have yet another boost::thread
called Thread C which will attempt to event_add using the C library
into Thread A's event loop and then immediately call event_active.

What I'd like to see after doing stuff from Thread C is this:

calling event_add from Thread C
calling event_active from Thread C
Processing events from Thread A
calling the event's handler from Thread A

Possibly we could have Processing events from Thread A anywhere else
between those lines, but for sure we must have at least 1 such line
between event_active and event handler execution, right?

What I see instead is this:

calling event_add from Thread X
calling event_active from Thread X
calling the event's handler from Thread A

Thread X is a different ID than Thread C but I guess that's just
because of using boost threads in the C++ calling site and pthreads in
the C library or something, anyways it doesn't bother me.

I also like the fact that my main purpose here is OK: firing the event
from a thread and handling it from another.

But for my own curiosity what I don't understand is why I don't see a
line "processing events from Thread A" before the handler callback is
called?

Thread ids were gotten with pthread_self(), this was tested on Linux,
more specifically Ubuntu 18.04.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.