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

Re: [Libevent-users] Help with progress thread



On Thu, Nov 11, 2010 at 8:00 PM, Ralph Castain <rhc@xxxxxxxxxxxx> wrote:
> Afraid I am hitting a brick wall, Nick. I can't make the threaded code work. Perhaps you have some further advice?
>
> I have attached my test code. Basically, it initializes the event lib, calls use_threads, creates a new base, and then spawns a progress thread that calls event_loop (you'll see that we prepended "opal" to your calls as part of an abstraction requirement - but I believe the calls are still understandable). The call to event_init is embedded in opal_init.
>

Here's a revised version of your test code.  The biggest change is
that I replaced all the opal calls with their libevent or pthread
equivalents so that I could actually run it on my little OSX box, and
rule out any possible bugs in the opal code.  (Not that there
necessarily _are_ any.)  The second-biggest change I made is that I
added error checking to all of the calls. This turned up a small bug:
you weren't specifying the third "mode" argument to open(), which is
required if you are using the O_CREAT flag.  For me, this resulted in
a "foo" file which I couldn't open after the first time, which meant
that my_fd would always be -1.

There's also a race condition in how you handle the fd_written
variable, but in practice it shouldn't matter in most places.

I also added a demo for how to use event_active() to wake up the
subthread, if that's something you're interested in.  Define the C
macro WAKE_WITH_EVENT if you're interested in trying that out.

When I built the revised code and linked it against libevent master using

   gcc -Wall -pthread -levent -levent_pthreads evthread-test.c -o evthread-test

it worked fine for me.  (That is, the write event fired message
appeared okay.)  This is on OSX 10.5.8.

If this code doesn't work for you, we'll have to start figuring out
why I'm misinterpreting the results, or how our systems differ.  If it
does work for you, we'll have to figure out whether your version is
failing because of something your library is doing differently from
straight libevent+pthreads, or because of the open() issue above, or
what.

yrs,
-- 
Nick

Attachment: evthread-test.c
Description: Binary data