Question

I'm considering two options to run asynchronous code: Qt Concurrent and std::async. Given that I'm writing a GUI app with Qt, it makes sense to go with Qt Concurrent. However std::async also seems good and supported by all major compilers.

Should I use Qt Concurrent or std::async for new code? What else should I look for when comparing the two?

Was it helpful?

Solution

Given that I'm writing a GUI app with Qt, it makes sense to go with Qt Concurrent

I would say, it is not so simple. I would personally utilize the standard library as much as I can. However, there are constraints to take into account:

Do you need to support your software on platforms not supporting at least C++11?

If the question is yes to that, using Qt solutions is a better option in a Qt based software. That being said, even for that, you could have different Qt solutions depending on your need. One is the thread weaver from KDE, but let us not go that far for now...

Another question, that you could ask from yourself:

Do you already have an existing code base where that is used throughout?

Depending on the answer, this could also provide further aspect for the decision, whether you prefer consistency, or forward thinking.

There is also another question here to be asked:

How much of QtConcurrent do I need?

Depending on the exact answer, it may or may not be a better alternative. Note that, not every functionality of QtConcurrent is in the standard library just yet, for instance something like QFutureWatcher with the Qt signal-slot mechanism.

So, yes, as a Qt user, I would suggest to use the standard library as much as possible. These days, Qt even explicitly depends on them, so will not run on platform not supporting it. Also, the general direction put seems to be this in Qt Project proper. For instance, lots of stuff got obsoleted in the QtAlgorithms, but that is just one of those.

OTHER TIPS

Qt Concurrent lets you run a function in another thread using QtConcurrent::run(). So i think you can only compare QtConcurrent::run() with std::async.

Qt Concurrent is so sophisticated and has many useful features. It includes APIs for parallel list processing and lets you create multi-threaded programs without using low-level threading primitives such as mutexes or semaphores. It also adjusts the number of threads used according to the number of processor cores available.

I think using Qt Concurrent is so cool because of its high-level APIs and ease of use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top