I'm pretty sure I use some form of handle events with timeout in the thread
that I use to handle events.  Each time the thread wakes up, it checks a
run flag and if the flag is reset, the thread exits.  When it wants the
event handler thread to quit, the main code resets the run flag and waits
for the thread to exit.

Yes, it costs a little cpu time for the thread to wake up every so often,
but I had a thread doing that anyway.  You also have to be careful about
which thread your callbacks are on, but the code I ported already had the
appropriate critical sections/mutexes.

Orin.


On Wed, Jul 3, 2013 at 8:10 AM, Chris McClelland <libusbx-de...@m3.ath.cx>wrote:

> Hi libusbx-devel,
>
> I'd like to write an application which uses the "simple" async approach.
> I can't use the polled approach because I'd like my code to be portable
> to Windows.
>
> Here's a scenario:
>
> The main thread creates the worker thread, which calls one of the
> libusb_handle_events_*() varients in a loop, ready to start handling
> completion events triggered by user actions.
>
> Ordinarily, the main thread would submit some requests to be handled by
> callbacks on the worker thread, but in this case the user merely asks
> for the app to shutdown immediately after startup. Therefore no requests
> are submitted, and the worker thread continues to block until the
> libusb_handle_events() timeout.
>
> In this scenario, what is the best way for the main thread to ask the
> worker thread to shutdown cleanly? It looks like I can call
> libusb_unlock_event_waiters() to force libusb_handle_events() to exit,
> but then I'd need a suitably-mutexed variable as a condition on the
> loop, and I start to get the feeling I'm doing something wrong.
>
> Alternatively, perhaps I must ensure the worker only enters
> libusb_handle_events() when it's sure there are outstanding requests,
> and I'm back to condition variables and I get that "I'm doing it wrong"
> feeling again.
>
> Any advice?
>
> Chris
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> libusbx-devel mailing list
> libusbx-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/libusbx-devel
>
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to