Results 1 to 8 of 8

Thread: Multiple threads accessing QList

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,333
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    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.

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

    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.

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

    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, 10:26
  2. Accessing Multiple Levels of QVariantMap
    By micgooapp in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2013, 10:43
  3. Replies: 2
    Last Post: 31st October 2007, 15:11
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  5. QT3: accessing singletons from within threads
    By karye in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2006, 11: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
  •  
Qt is a trademark of The Qt Company.