Qt-interest Archive, May 2007
Qt containers thread safety question
Message 1 in thread
Hi There,
I tried to find in the docs and googled for it too and am still not sure; so
are Qt container classes thread safe? Or do I need to protect them with
mutexes and such?
For example, I have a QList<int>. Items added to this list by two threads
and three other threads pull out items from the thread. Do I need to add
mutexing for every single push_back and take_first or not?
Thanks,
Sandor
Message 2 in thread
On Friday 18 May 2007 15:30:10 Hadas Sandor wrote:
> Hi There,
>
> I tried to find in the docs and googled for it too and am still not sure;
> so are Qt container classes thread safe? Or do I need to protect them with
> mutexes and such?
>
> For example, I have a QList<int>. Items added to this list by two threads
> and three other threads pull out items from the thread. Do I need to add
> mutexing for every single push_back and take_first or not?
Yes, you do.
Classes in the documentation will be documented as thread-safe only if they
are intended to be used by multiple threads. For example,
http://doc.trolltech.com/4.3/qmutex.html has a Note: after the #include
statement:
"Note: All the functions in this class are thread-safe."
Individual functions will be documented as thread-safe as well, as needed:
http://doc.trolltech.com/4.3/qcoreapplication.html#postEvent
--
[ signature omitted ]
Message 3 in thread
From the docs:
The container classes are implicitly shared, they are reentrant, ...
...
Throughout the Qt documentation, the terms reentrant and thread-safe are
used to specify how a function can be used in multithreaded
applications:
* A reentrant function can be called simultaneously by multiple
threads provided that each invocation of the function references unique
data.
* A thread-safe function can be called simultaneously by multiple
threads when each invocation references shared data. All access to the
shared data is serialized.
In short, no - they are not thread-safe. Also note that some iterators
may become invalid when the containers are modified.
Cheers,
Peter
> -----Original Message-----
> From: Hadas Sandor [mailto:Sandor.Hadas@xxxxxxxx]
> Sent: Friday, May 18, 2007 3:30 PM
> To: 'qt-interest@xxxxxxxxxxxxx'
> Subject: Qt containers thread safety question
>
> Hi There,
>
> I tried to find in the docs and googled for it too and am still not
sure;
> so are Qt container classes thread safe? Or do I need to protect them
with
> mutexes and such?
>
> For example, I have a QList<int>. Items added to this list by two
threads
> and three other threads pull out items from the thread. Do I need to
add
> mutexing for every single push_back and take_first or not?
>
> Thanks,
> Sandor
--
[ signature omitted ]