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

Roland Hughes roland at logikalsolutions.com
Fri Oct 27 21:03:35 CEST 2017



On 10/27/2017 01:20 PM, interest-request at qt-project.org 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.
One must be __very__ careful when tossing around the phrase "read only" 
when speaking of QMap.

http://doc.qt.io/qt-5/qmap.html#operator-5b-5d


      T&QMap::operator[](constKey&/key/)

Returns the value associated with the key/key/as a modifiable reference.

*If the map contains no item with key****/key/**, the function inserts 
a*default-constructed value 
<http://doc.qt.io/qt-5/containers.html#default-constructed-value>into 
the map with key/key/, and returns a reference to it. If the map 
contains multiple items with key/key/, this function returns a reference 
to the most recently inserted value.

Even if you only intend to read [] has modify capability.
http://doc.qt.io/qt-5/qmap.html#value


      constTQMap::value(constKey&/key/, constT&/defaultValue/= T()) const

Returns the value associated with the key/key/.

If the map contains no item with key/key/, the function 
returns/defaultValue/. If no/defaultValue/is specified, the function 
returns adefault-constructed value 
<http://doc.qt.io/qt-5/containers.html#default-constructed-value>. If 
there are multiple items for/key/in the map, the value of the most 
recently inserted one is returned.

Notice the doc makes no hint about value() having write access to the 
data. Could lull someone into thinking it is safe across threads.

Let us not forget Qt's copy-when-needed shared data. I jumped into this 
late and didn't see the code for the QMap. If the value is an integer 
you will be fine, but, it used to be even a QString delayed copy until 
it had to so even something like:

QString myLocalStr = myMap.value("blah");

could create this time bomb when myMap exists in a different thread and 
the copy source/dest addresses could move before "when-needed" occurred. 
Used to be a problem anyway. I've modified my designs so long ago I just 
never run into these issues anymore.




-- 
Roland Hughes, President
Logikal Solutions
(630)-205-1593

http://www.theminimumyouneedtoknow.com
http://www.infiniteexposure.net
http://www.johnsmith-book.com
http://www.logikalblog.com
http://www.interestingauthors.com/blog
http://lesedi.us/
http://onedollarcontentstore.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171027/3129a9ae/attachment.html>


More information about the Interest mailing list