Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, October 2007
qlist append question


Message 1 in thread

Hi all, is it safe to assume that if i have a QThread that reads a
QList i can safely use the append() function from another thread? i.e.
i expect that maybe the first QThread reads fewer elements from the
list.

thanks a lot

--
 [ signature omitted ] 

Message 2 in thread

On 09.10.07 20:36:52, Francesco Lamonica wrote:
> Hi all, is it safe to assume that if i have a QThread that reads a
> QList i can safely use the append() function from another thread? i.e.
> i expect that maybe the first QThread reads fewer elements from the
> list.

Does the API documentation of the function explicitly state that this is
threadsafe? If not then no you can't do that.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

Francesco Lamonica wrote:
>Hi all, is it safe to assume that if i have a QThread that reads a
>QList i can safely use the append() function from another thread? i.e.
>i expect that maybe the first QThread reads fewer elements from the
>list.

Not if it's the same list.

QLinkedList can do that, since it doesn't move elements when adding. No 
iterators will be broken. However, the operation isn't atomic, so it 
could break if you're iterating over the list in another thread. So, 
iterators are safe, iterating isn't.

QList can't do it because it is in fact a vector of pointers to your items 
(i.e., QList<T> is similar to QVector<T*>). So any append() operation can 
potentially cause the list to grow and, therefore, realloc() everything.

If you instead have two QList objects, then appending is thread-safe, even 
if the two are sharing the same list.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.