[Qt-interest] thread safety of Qt containers

Andre Somers andre at familiesomers.nl
Mon Apr 26 12:14:56 CEST 2010


Hi,

Thanks for your answers so far.

On 26-4-2010 11:41, Thiago Macieira wrote:

> Em Segunda-feira 26 Abril 2010, às 11:26:17, Andre Somers escreveu:
>    
>> Hi,
>>
>> I was wondering:
>> Qt's documentation states that the methods in QList, QVector and QHash
>> (and maybe QMap, I did not check) are reentrant. That means that they
>> can be used in a thread, as long as the instance that is used is not the
>> same, right?
>>      
> Correct.
>
>    
>> So... how does QtConcurrent work? QtConcurrent::map() and the likes have
>> multiple threads operating on data in the same container. How save is
>> that? The docs say that only the non-in place operations make a copy of
>> the container, so that would mean that multiple threads at the same time
>> can write to a QList? They can't be using a QMutableListIterator though,
>> as only one of those can be savely operating on a list at the same time...
>>      
> There's a mutex. That ensures that there's only one thread accessing the
> container at a time.
>    
So, performing a very simple operation using QtConcurrent is not that 
efficient, because of the locking and unlocking of the mutex? I seem to 
recall that mutexes are relatively expensive resources to aquire, right?

>> I am looking into QtConcurrent to speed up some heavy calculation work
>> that I need to do. However, the amount of data is such that copying
>> around data is really not an option. It is simply too much (datasets may
>> range into the GB range).
>>      
>
Would it be save to read from the same datastructure from multiple 
threads, if not write to them?

André




More information about the Qt-interest-old mailing list