I was hoping someone with more experience could shed some light on
using Pth with the main event loop of a GUI.

The two things I'm having trouble with are

    (1) how to make sure the gui stays responsive when the threads are
        busy, and

    (2) how to efficiently do nothing when the threads don't have
        anything to do and the gui is idle.

For (1), from what I can tell from the Pth source code, it seems that
most of the blocking calls like read() and select() have been
#defined-diverted to calls like __pthread_read() and
__pthread_select().

I've sort of run out of time for this weekend, but it seems that these
calls try non-blocking I/O and if that fails, they add themselves to
the Pth event queue, save their machine context, and give control back
to the scheduler.  I didn't make it past here in the code, but my
guess is that the Pth event queue is select()- or poll()-based.  When
the file descriptor is ready, the scheduler will restore the machine
context and give control back to the thread.

So as long as my non-gui threads once in a while either call
sched_yield() or hit a blocking call that has been diverted to a
__pthread_*() call, the gui will stay responsive.

But, the gui's main event loop invariably calls select() directly so
that, once the GUI's main event loop thread is given a time slice
(which happens fairly quickly because Pth thinks its runnable), it
simply blocks until the next gui event causing the non-gui threads to
starve in the meantime.

If I add a timeout to the gui event loop in order to yield the
scheduler, I then get into the problem of (2) above in that I'm busy
waiting when all the threads are blocked on I/O.

Is there anything I can do short of writing my own gui event loop that
calls __pthread_select()?

Thanks,
Paul


P.S.  I have attached the sample code I was using for testing.  Oddly
enough, the behavior when I block on read() is different under OpenBSD
than it is under Linux.  Under OpenBSD, the call the read() yields the
scheduler, but under Linux, it doesn't.  I'm using the version of Pth
that comes with OpenBSD 2.9.  For Linux, I compiled from scratch and
all the tests were a success.  The Linux kernel I'm using is
2.4.18-rc2.

Attachment: pthreads_preemption_test.tar.gz
Description: GNU Zip compressed data

Reply via email to