Hi,

On 07/03/2013 05:10 PM, Chris McClelland 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,

Erm, no that won't work. libusb_unlock_event_waiters() unlocks an internal
mutex, and does nothing else. So you should never call it unless you've
first called libusb_lock_event_waiters(), and even if you first call that,
it won't do what you want (it is used if you want to do some complicated
event-handling stuff yourself, see the async docs).

I've the exact some problem in spice's usbredir connection code, and
what I do there (*) is I only start the event thread as soon as I make
my first libusb_open call, and I stop the thread when I make the
libusb_close call which drops my open device count from 1 -> 0.

The closing part is somewhat tricky, you need to do the following:

void *event_thread_func(void *arg)
{
     while (thread_run)
         libusb_handle_events(ctx);
}

void my_close_handle(libusb_device_handle *handle)
{
     if (open_devs == 1)
         thread_run = 0;

     libusb_close(handle); /* This causes libusb_handle_events() to return 
*once* */

     if (open_devs == 1)
         pthread_join(event_thread);

     open_devs--;
}


I hope this helps, if not don't hesitate to ask for more info!

Regards,

Hans



*) I did it there to not unnecessarily start a thread, but I now realize
it also solves the problem you're describing.

------------------------------------------------------------------------------
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