我是libusb的新手,所以我不太了解它。我正在尝试做一些USB通信。Libusb在使用C++ 11异步时挂起
我使用的热插拔功能工作得很好。所以我想当我检测到一个设备到达事件时,我会在另一个线程上使用C++11异步功能与USB进行所有通信,因此我可以使用多个设备进行同步I/O以简化代码。他们的异步I/O对我来说有点混乱。希望我可以在C++异步功能中使用同步I/O。
但我有一个问题,当代码运行在C++ 11异步功能中时,某些libusb调用似乎挂起。当它没有运行在C++ 11异步特性中时,它没有任何问题。
所以我假设它是我的C++ 11异步功能代码,这就是问题所在。
这里是我的热插拔回调:
Opening
Opened
Closing
主要的代码如下所示:
int main(int argc, char* argv[]) {
int vendor_id = 0x1234;
int product_id = 0x4556;
libusb_hotplug_callback_handle *hp = nullptr;
libusb_context *context = nullptr;
int rc = libusb_init(&context);
if(rc < 0)
{
return rc;
}
libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING);
rc = libusb_hotplug_register_callback(
context,
LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
LIBUSB_HOTPLUG_NO_FLAGS,
vendor_id,
product_id,
LIBUSB_HOTPLUG_MATCH_ANY,
hotplug_callback,
NULL,
hp
);
if (LIBUSB_SUCCESS != rc) {
libusb_exit (context);
return rc;
}
while(1) {
rc = libusb_handle_events(context);
}
return 0;
}
int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
std::future<void> result(std::async([] (libusb_device *d) {
libusb_device_handle *h;
printf("%s\n", "Opening");
int rc = libusb_open(d, &h);
if(rc != LIBUSB_SUCCESS) {
printf("ERROR: %s\n", libusb_error_name(rc));
return;
}
printf("%s\n", "Opened");
printf("%s\n", "Closing");
libusb_close(h);
printf("%s\n", "Closed!");
}, dev));
result.get();
return 0;
}
与此代码它挂在
libusb_close
它输出
所以
请注意,这段代码更具原型性,所以它的写法不是很好。它仍处于libusb的探索模式。
谢谢,我终于找到了他们的异步API – pdiddy