Libusbpp icon indicating copy to clipboard operation
Libusbpp copied to clipboard

Asynchronous/Threaded Transfers

Open gggrafff opened this issue 4 years ago • 3 comments

Good afternoon. The wording of the name of this example is a bit confusing https://github.com/zarthcode/Libusbpp/blob/389ceea9bc974f1c2b1cf3151a32795c04832288/examples/LibusbTest.cpp#L355 I think it is not necessary to mix the concepts of asynchronous and multi-threading. Asynchronous is not about creating parallel threads, but switching between tasks within a thread. Asynchronous can be implemented using coroutines, which were added in C++20. Example: If we need to make simultaneous requests, then we can create separate threads for each request. All these threads will be waiting for a response, this is not very efficient if waiting for a response takes a significant part of the time. In this case, it is more efficient to create requests in one thread, and while waiting for a response to one request, process the response to another request. This will allow you to spend less time waiting. In addition, switching between threads is much more expensive than switching between coroutines.

Also, I find it not very efficient to create and delete threads for each new request if the requests need to be sent frequently.

What do you think about it? Thanks

gggrafff avatar Dec 21 '20 18:12 gggrafff

libusb has asynchronous transmission. We only need to make a wrapper for it. http://libusb.sourceforge.net/api-1.0/group__libusb__asyncio.html

gggrafff avatar Jan 02 '21 19:01 gggrafff

We can use std::async also instead of creating a new thread for each new transfer. It may be implemented as thread pool sometimes. Or we can implement own thread pool. It will help reduce the cost of creating threads and improve performance.

gggrafff avatar Dec 07 '21 12:12 gggrafff

This will be an important improvement for libusbpp.

mcuee avatar Feb 11 '23 13:02 mcuee