OK. I've found the answer.
After re-reading the api at
http://libusb.sourceforge.net/api-1.0/g ... yncio.html the libusb library does not start any threads for handling events in the background. Hence it is up to the user to call the event handling functions at the correct times.
For this there is three conditions:
1) When an event occurs such as read/write using a libusb_fill_####_()
2) When a timeout occurs for a read/write using a libusb_fill_####_()
3) When an event handling is required at a specified time
So the simplest way to overcome this is to create a thread which calls libusb_handle_events(). This function does block until its needed to process, thus having a separate thread for handling this function should not take up too much processing power.
I think the proper way is to integrate libusb_handle_events_timeout() into your application where it is called at all when either of those above 3 conditions occur.
Code: Select all
void * MyLibusbEventsHandler(void * threadarg)
{
mymutex * args = (Mymutex *) threadarg;
while (*(args->running))
{
pthread_mutex_lock(&args->handle_mutex);
printf("Handle: Handle thread sleeping.\n");
pthread_cond_wait(&args->handle_condition, &args->handle_mutex);
pthread_mutex_unlock(&args->handle_mutex);
printf("Handle: Handle thread started.\n");
while (args->device)
{
libusb_handle_events(args->context);
printf("Handle: Events detected.\n");
}
}
}
So the above example shows how I've simply implemented a thread which will loop continuously until:
1) there is no device available for handling eg. device disconnected (args->device)
2) the user is closing the program, thus manually removes the device parameter and turns args->running off
So in short, you HAVE to include some sort of calling to libusb_handle_events() during processing. This will handle your file descriptors also.