Results 1 to 8 of 8

Thread: Multiple threads accessing QList

  1. #1
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Multiple threads accessing QList

    Hello all,

    I know that Qt containers (QList in my case) are not thread-safe but they support reentrancy.

    I have 3 threads:
    • DataGenerator thread
    • UI thread
    • DataToDisc thread


    Their responsibility:
    • DataGenerator thread appends the generated data to a QList.
    • UI thread requests the latest data whenever it feels like!
    • DataToDisc thread reads from the beginning and writes the data to disc. It also removes this item from the list (to avoid large lists over time). This is done by using QList::takeFirst(). It does its job only if there are at least 'n' elements in the list. So I am not accessing the same element from multiple threads.


    So my question is: Is this approach wrong? I get occasional crashes with the takeFirst(). Could that be because this function causes a list "reordering" or something thereby causing undefined behavior.

    Should I not use QList for this purpose?

    Thanks

    Regards
    Vikram

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple threads accessing QList

    Hi,

    Use a QMutex to prevent the concurrent access to the list.
    Òscar Llarch i Galán

  3. #3
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple threads accessing QList

    Hi NyAw,

    Quote Originally Posted by ^NyAw^ View Post
    Use a QMutex to prevent the concurrent access to the list.
    Thanks for the quick response. Performance is very crucial to me, I can use a Mutex, ReadWriteLock etc. for sure. But the approach I suggested was an attempt to avoid such locking.. any input on why that would/wouldn't work would be much appreciated

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple threads accessing QList

    QList won't do without a mutex. You need a lock-free queue, but that only works as long as all you do is atomically push and pop elements. You are pretty much doomed if your UI thread can inspect the whole structure; for instance, imagine that it is inspecting an element and suddenly the DataToDisc thread pops and deletes that element...

  5. The following user says thank you to yeye_olive for this useful post:

    Vikram.Saralaya (8th January 2016)

  6. #5
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple threads accessing QList

    Thanks yeye.. But how can I atomically push and pop elements?

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple threads accessing QList

    I think yeye_olive is trying to tell you that you can't push and pop atomically using a QList - in a multithreaded app, you need to protect access using a mutex if you want to ensure a writer doesn't corrupt what a reader is looking at.

  8. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple threads accessing QList

    Indeed, general-purpose containers such as QList do not atomic push and pop operations. If you want to avoid synchronization with mutexes as much as possible, you need specialized containers; see, for instance, the Boot.Lockfree library.

    However, the fact that the UI thread can inspect the whole container means that you will probably need heavy synchronization.

  9. #8
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple threads accessing QList

    Thanks Yeye and d_stranz. Its good to know that its not trivial to atomically push and pop in any of the available qt containers.

    Mutex isn't an option to me since it does add a lag to the UI thread.

    For anyone interested in how I dealt with this:
    I ended up creating a custom circular buffer with QAtomicInteger write pointer. I preallocate memory so my DataToDisc thread now just acts like a reader (without worrying about the growing list). Readers can never read beyond the write pointer and hence no corrupt data will ever be read. Works well!

    Regards
    Vikram

Similar Threads

  1. Accessing same variable from multiple windows
    By harvey_slash in forum Newbie
    Replies: 21
    Last Post: 5th May 2016, 11:26
  2. Accessing Multiple Levels of QVariantMap
    By micgooapp in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2013, 11:43
  3. Replies: 2
    Last Post: 31st October 2007, 16:11
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 21:43
  5. QT3: accessing singletons from within threads
    By karye in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2006, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.