How to properly use the asynchronous libusb?
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
Code:
libusb_fill_control_transfer(transfer, dev, buffer, cb, &completed, 1000);
libusb_submit_transfer(transfer);
while (!completed) {
libusb_handle_events_completed(ctx, &completed);
}
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
Code:
// I create the thread when the application starts
QFuture<void> usbEventHandlerPoller;
usbEventHandlerPoller = QtConcurrent::run( usbConnection, &USBConnection::PollUSBEventHandler, &threadAbortFlag );
// Here the cuncurrent thread with the infinite loop
void USBConnection::PollUSBEventHandler( volatile bool* pThreadAbortFlag )
{
int completed = 0;
while ( 1 )
{
libusb_handle_events_completed( NULL, &completed );
if ( *pThreadAbortFlag == true )
{
return;
}
}
}
Can anyone, with experience in the asynchronous features of libusb library, give some suggestions on the right approach to handle the transfer events?