[Interest] QMap crash when using QtConcurrent::blockingMapped()

Elvis Stansvik elvstone at gmail.com
Fri Oct 27 18:43:57 CEST 2017


2017-10-27 15:52 GMT+02:00 Jason H <jhihn at gmx.com>:
>> Sent: Friday, October 27, 2017 at 6:44 AM
>> From: "Konstantin Shegunov" <kshegunov at gmail.com>
>> On Fri, Oct 27, 2017 at 9:45 AM, Elvis Stansvik <elvstone at gmail.com[mailto:elvstone at gmail.com]> wrote:
>
>> > QMap isn't thread safe, so I don't think your use is guaranteed to be safe, even if you're only reading.
>
>> You're right, in the formal sense of the word, but if there are no `mutable` members or `const_cast` usage a reentrant class can be used from different threads IF the calls are read-only. I sometimes use `QHash` like this where I need to read from a pre-populated hash from different threads, with the understanding there's no need to lock something if the memory is never changed.
>
>
> I think that is the operative definition of "reentrant"? "Thread-safe" would imply mean that any mutating operations would succeed and leave things in a consistent state, usually via mutex. Reentrant would imply the same function can be called in an overlapping manner. Reentrant and thread safe means that both are true. The QMap documentation explicitly states that it is not thread safe, but it is reentrant. Which is how I am using it and why I posted. Where I did mess up is in using non-const [] which can cause mutations, though it's use in my situation did not need to, so I did not expect it to, but it did anyway. :shrug:

The definition of reentrant that Qt's documentation uses is documented
at http://doc.qt.io/qt-5/threads-reentrancy.html.

It's a little different from the one used in for example the POSIX C API.

Reentrant in the object oriented context of Qt means: It's safe to
call member functions from different threads _if_ the calls are made
on different instances.

In your case you were making calls on the same instance.

You may be getting away with it by only calling const member
functions, and hoping that the class isn't modifying some `mutable`
stuff in its implementation. But the implementation isn't documented,
so it's by no means guaranteed AFAIK.

Elvis

>
> So getting back to your statement "I don't think your use is guaranteed to be safe, even if you're only reading." I think *is* guaranteed to be safe by the documentation, as long as I use it correctly. Anyway, just wanted to point that out if you missed it in the docs. :-) Knowing it's reentrant can be a big help.



More information about the Interest mailing list