Reputation: 4459
The doc says the QList is reentrant. So for the following code is it thread safe?
//worker thread
...
if(myList.size() == 0)
{
...
}
//main thread
foreach(QImage, myList)
{
...
}
All the ... parts have nothing to do with myList. The myList object in two threads are the same object. So is this thread safe?
Upvotes: 0
Views: 553
Reputation: 9292
If myList is const and therefore all accesses are read only, this is thread safe.
But if in at least one thread code is doing non-const access on the object, this is not thread safe.
To be sure that you are doing read-only operations, declare a const reference on myList and use only this one in concurrent code :
const QList<QImage> & constMyList = myList;
This has nothing to do with reentrancy. Reentrancy tells you that if you are doing operations (read or write) on two different QList instances in two different threads respectively, the behavior is defined.
For instance, a non-reentrant class may use static functions/members in non-static methods. If these static functions/members are not guarded, the class methods will not be reentrant : even when working on two independent objects, the behavior can be undefined.
However, the Qt doc says you that read-only operations on containers is thread-safe. This is what you are looking for.
Upvotes: 3