Libuv event loop in qt

Is there a way without using multiple threads?

I found this StackOverflow question.

But I don't understand how it works. Shouldn't this result in 100% CPU usage in this example? and how can I embed it in eg QT?

there is this too : https://github.com/svalaskevicius/qt-event-dispatcher-libuv But there is no documentation at all. But from my views it looks like it is being translated from the QSocket example to uv_tcp_socket, which is not what I'm looking for.

+3


source to share


1 answer


In short, you will need to combine the two event loops, or use separate threads and manually synchronize the event handlers.

The first link you attached shows how to handle libuv events that have occurred since the last call. Whereas it is stated that ~ 100% CPU will be used if there are no events sent (as it will just keep polling).

The second link (qt-event-dispatcher-libuv) is a project I created to solve the same problem. However, it works as you described, using libuv to handle the Qt event loop (and thereby merging the two event loops into one).

To use it, you just need to register an event dispatcher in your application using http://qt-project.org/doc/qt-5/qcoreapplication.html#setEventDispatcher . An example of using this library - https://github.com/svalaskevicius/qtjs-generator/blob/master/src/runner/main.cpp#L179



There is still one catch using this approach - although it works great on Linux, there are some issues in OS X as the Qt Cocoa platform support plugin handles some of the Cocoa event loop operations and does not provide a good API to merge it (currently its updating them is freed after a little timeout, so there are some (hardly) noticeable delays for handling GUI events). I was planning to include a platform support plugin to be able to integrate it as well, but that is still in the future. And I haven't tested it on windows yet :)

Perhaps an alternative solution could be to try to merge the two loops from the other direction, which I did - instead of forcing Qt to use libuv, the libuv api can be provided which uses Qt handlers, although it takes a significant amount of work too.

Please let me know if there is more information I could provide.

Hello,

+4


source







All Articles