I worked on the synchronous libusb in my Qt project with good results and now I need the asynchronous features of this library. I understood reading here, here and here that, after I've registered my callback function using the libusb_fill_control_transfer and submitted a transfer with libusb_submit_transfer , I need to "keep live" the libusb_handle_events_completed inside a while loop to get the transfer related events since the libusb doesn't have its own thread. For example you can read a code like this

Qt Code:
  1. libusb_fill_control_transfer(transfer, dev, buffer, cb, &completed, 1000);
  2. libusb_submit_transfer(transfer);
  3. while (!completed) {
  4. libusb_handle_events_completed(ctx, &completed);
  5. }
To copy to clipboard, switch view to plain text mode 

Now if I want read a packet that I don't know when it occurs, I think that goes against the asynchronous nature submit a read and wait in the while with libusb_handle_events_completed until the event is triggered.

Then, do I need to create a separate thread within the libusb_handle_events_completed in an infinite while loop? Like

Qt Code:
  1. // I create the thread when the application starts
  2. QFuture<void> usbEventHandlerPoller;
  3. usbEventHandlerPoller = QtConcurrent::run( usbConnection, &USBConnection::PollUSBEventHandler, &threadAbortFlag );
  5. // Here the cuncurrent thread with the infinite loop
  6. void USBConnection::PollUSBEventHandler( volatile bool* pThreadAbortFlag )
  7. {
  8. int completed = 0;
  10. while ( 1 )
  11. {
  12. libusb_handle_events_completed( NULL, &completed );
  13. if ( *pThreadAbortFlag == true )
  14. {
  15. return;
  16. }
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 

Can anyone, with experience in the asynchronous features of libusb library, give some suggestions on the right approach to handle the transfer events?