[Interest] QMap and thread-safe.

Bo Thorsen bthorsen at ics.com
Tue Jul 23 14:46:36 CEST 2013


Den 23-07-2013 13:34, Giuseppe D'Angelo skrev:
> On 23 July 2013 13:25, Bill Crocker <william.crocker at analog.com> wrote:
>> Hi all:
>>
>> The QMap class states that all member functons are reentrant, but
>> if I populate a map at program start-up (with QStrings, let's say) and I
>> do not change its contents from then on... I should be able to treat the
>> member function ::contains() and any other member function which does not
>> modify the map as thread-safe.
>>
>> Is true?
>
> Probably yes, although totally undocumented. (There has been some
> discussion before the release of 5.0 -- about whether we should make
> it official that using only "const" methods on containers makes them
> thread safe, but I don't remember the outcome...)

If you make sure only to call const methods, I think you should be fine. 
There's no lazy evaluation in QMap, so I don't see any reason this 
shouldn't work.

But if this suddenly change, you are going to have a horrible bug on 
your hand. And since it would be a race condition bug, it's not 
something you can unit test your way out of.

If you want to be 100% certain, I suggest you do your own simple map 
implementation. It's easy enough to do, if you don't have a huge set of 
requirements. And you can probably use the QMap hashing, I'm pretty sure 
that is thread safe.

Whether you want to go to this or take the chance probably depends on 
how critical this code is.

Bo.

-- 
Bo Thorsen, European Qt Manager, Integrated Computer Solutions
ICS - Delivering World-Class Applications for Embedded & Mobile Devices
http://ics.com/services



More information about the Interest mailing list