Not safe to use QPixmap outside the GUI thread
-
Yeah, I've seen threads about this. Dozens. They are all pretty short, and end with "don't do this - use QImage".
Fine. However...
I investigate such a case. An interestingly, although the warning is displayed, I haven't seen any bad effects. With dozens of users causing the warning for the past few weeks. Both on Windows and Linux. And nobody misses any pixmaps, so they are all there.
So I wonder if the warning is indeed still legit. Of course, I wouldn't try to paint a pixmap in any other thread but the GUI thread. But reading it from file, and storing it in an object? It seems to work just fine.
So could someone shed more light on what really happens to a QPixmap outside the GUI thread, and why it's not a good idea to use it there?
-
Hi,
@Asperamanca said:
I haven't seen any bad effects. With dozens of users causing the warning for the past few weeks. Both on Windows and Linux. And nobody misses any pixmaps, so they are all there.
That's called "undefined behaviour". It works in the cases you've observed, but the designers have not made any attempt to guarantee that it will always work. The designers might change the internals in the future in a way that causes it to stop working.
So I wonder if the warning is indeed still legit. Of course, I wouldn't try to paint a pixmap in any other thread but the GUI thread. But reading it from file, and storing it in an object? It seems to work just fine.
So could someone shed more light on what really happens to a QPixmap outside the GUI thread, and why it's not a good idea to use it there?
Interesting question. I recommend asking at the Interest mailing list, where the Qt engineers hang out.
If you're feeling adventurous, have a look through the QPixmap source code: http://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.h.html (it's interactive -- clicking a function in the header will take you to its implementation, for example)
One thing I noticed is that QPixmap objects read/write global objects without mutexes or any other thread locking.
-
As stated in the docs and in contrast to QImage, QPixmap has an underlying platform specific storage implementation. So whether or not it's safe to use QPixmap in a worker thread partially depends on that platform specific underlying type. It might be the case that on some platforms you can't move that type between threads easily. It might also well be that most operations are safe in your particular case, but something like detach(), where the data needs to be copied, might not be thread safe or unsupported between threads on some platforms.
My guess is some of the operations could be assured to work. It's just that the code is playing it safe. I'm not surprised by that knowing the multitude of platforms supported and the fact that even on one platform the backends may vary (e.g. the experiments with OpenGL backend in Qt 4 where making textures thread safe and copyable could be problematic).
But as JKSH said you might get more gritty details from the devs.
-