From: Orin E. <ori...@gm...> - 2011-03-28 16:54:20
|
On Mon, Mar 28, 2011 at 7:57 AM, LarryM <lmi...@di...> wrote: > > > I am thinking about creating a special thread which would run in a loop > calling libusb_handle_events(). The threads for each endpoint would submit > their transfer requests and then wait on a semaphore which would passed to > libusb_fill_bulk_transfer() and released (sem_post()) by the transfer > completion callback. Each endpoint/thread would have its own semaphore. > The tricky part would be knowing when the new thread should call > libusb_handle_events(). If it calls libusb_handle_events() at a time when > no transfers are pending, it will not return for 60 seconds, which could be > a problem when shutting down a connection. > Use libusb_handle_events_timeout() in a loop with a timeout of say, 100 mS. Use a flag to terminate the loop. Something like: struct timeval tv; // Set tv to 100 mS while ( runFlag && tranfersOutstanding ) { libusb_handle_events_timeout(context, &tv); } Increment transfersOutstanding when you submit a transfer, decrement it when you get a completion callback. I use something similar, except I also use the thread to provide a timer, so I have to juggle the timeout value. The 100mS of course is a tradeoff between increased CPU usage and how long you are prepared to wait at shutdown. Orin. |