Results 1 to 4 of 4

Thread: Access to TableModel methods from other thread

  1. #1
    Join Date
    Feb 2016
    Posts
    2
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Access to TableModel methods from other thread

    I am skimming through my code and noticed a circumstance where I'm not sure whether it is potentially dangerous.

    I have a QTableView to visualize a download queue. It uses a custom QAbstractTableModel subclass (using pyqt5 here) :
    Qt Code:
    1. class DownloadQueueTableModel(QAbstractTableModel):
    2.  
    3. downloadQueue = deque()
    4. queueLock = Lock()
    5.  
    6. def addToQueue(self, item):
    7.  
    8. row = self.queueLength()
    9. self.beginInsertRows(QModelIndex(), row, row)
    10.  
    11. with self.queueLock:
    12. self.downloadQueue.append(item)
    13.  
    14. self.endInsertRows()
    15.  
    16. def getNextItem(self):
    17. if self.queueLength() == 0:
    18. return None
    19. self.beginRemoveRows(QModelIndex(), 0, 0)
    20. with self.queueLock:
    21. item = self.downloadQueue.popleft()
    22. self.endRemoveRows()
    23. return item
    To copy to clipboard, switch view to plain text mode 

    My table model uses an internal queue to save the data. While the addToQueue method is called as a reaction to a button click - and therefore in the context of the UI thread - the getNextItem method is called from a worker thread, which downloads the items one after the other.
    The access to the queue data structure itself is protected through a lock and therefore thread safe, the only question that remains:
    Is a call to beginRemoveRows and endRemoveRows from another thread, which is not the UI thread, safe?

    The application is running correctly and I have not noticed any problems so far, but we all now that errors caused by racing conditions are very random and hard to find later on.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to TableModel methods from other thread

    I assume you also have the lock in rowCount() and data(), but in general that is not safe.

    What you could consider instead is the thread emitting a signal when it is ready for a new item and connecting that to a model slot using Qt::QueuedConnection as the connection type.
    That slot is then called by the main thread which could then dequeue the item and send it to the worker thread (either again via a QueuedConnection signal or by calling a method that internally uses a lock to protect against concurrent access)

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ichmeins (27th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    2
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: Access to TableModel methods from other thread

    Ok, thanks for the clarification.
    I'll let the worker thread just read the first position in the queue and signal the UI thread to remove the item. That would only require a one way communication between threads.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to TableModel methods from other thread

    True, but that still requires your to lock the queue and the worker could work on the same item multiple times.

    Cheers,
    _

Similar Threads

  1. How to access OCX methods and events
    By joy in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2018, 16:37
  2. How to access to methods of parent class
    By tonnot in forum General Programming
    Replies: 3
    Last Post: 29th June 2011, 07:04
  3. Replies: 10
    Last Post: 10th February 2011, 19:45
  4. thread GUI access crash
    By liqxpil in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2010, 15:47
  5. cannot access QLineEdit text in other methods of the class...
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 12:07

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.