Results 1 to 2 of 2

Thread: Odd QMap/QList possible thread problem

  1. #1
    Join Date
    Nov 2008
    Posts
    180
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Odd QMap/QList possible thread problem

    This is odd.

    I have a model which supports a table. The model is owned by the application class and exists in the GUI thread. The database access all happens in a different thread to avoid locking the GUI. There is now another thread which connects to the model for use with data extraction. The data is chunked into this model for other reasons. When utilized in the GUI by QTableViews I can chunk and scroll to my heart's content. The extract thread will sometimes process the entire database range N times without issue. Other times it will gag a seg-fault in the exact same place with different index values. Adding insult to injury, I can open up the object which was indicated by the index and see the data.

    Qt 4.8.x on 32-bit Linux for embedded target.

    It always dies right here: line 183 qmap.h
    inline QMap(const QMap<Key, T> &other) : d(other.d)


    QVariant ExportModel::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    {
    return QVariant();
    }

    ResultCell cell = m_rows[ index.row()][ index.column()]; // dies right here both row and column completely valid and m_row[][] data viewable in debugger

    return cell.data( role);
    }

    m_rows is nothing complex

    QList< QList< ResultCell> > m_rows; // list of rows, each row is a list of cells


    ResultCell is no great shakes either:

    class ResultCell
    {
    public:

    bool setData( const QVariant& value, int role=Qt:isplayRole)
    {
    m_data.insert( role, value);
    return true;
    }

    QVariant data( int role) { return m_data[ role]; }

    private:
    QMap< int, QVariant> m_data;
    };

    I guess my question is, can a model based on QAbstractTableModel be shared across threads? That would explain the very oddness of this problem. I've been monitoring with top and it isn't a memory leakage issue. Thought I would ask before I created another instance of this model out in the export thread.

    Thanks

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Odd QMap/QList possible thread problem

    Most Qt classes are reentrant, but not thread safe. I believe you need to serialize access to QMap/QList by using a QMutex or QMutexLocker convenience function.

    Edit: Each Qt class has a note just past the class overview that states whether the class is reentrant or thread safe. If the class is reentrant, then you need to serialize access to the instance of the class on your own. If the the class is documented as thread safe, then the class itself serializes resources to ensure they can be safely used by multiple threads simultaneously.
    Last edited by jefftee; 18th February 2015 at 20:44.

Similar Threads

  1. Replies: 1
    Last Post: 6th February 2014, 08:28
  2. pointer to nested QMap and QList
    By franki in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2013, 11:44
  3. QMap in QList | i can get value but i can`t set value
    By petrusPL in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2013, 18:40
  4. QMap QList
    By dare5421 in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2011, 23:17
  5. QMap in QList ?
    By npc in forum Newbie
    Replies: 2
    Last Post: 5th February 2007, 11:51

Tags for this Thread

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.