Results 1 to 10 of 10

Thread: QtConcurrent locking

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QtConcurrent locking

    I have a QtConcurrent function that manipulates list items that can become deleted at any time. I made in if statement to check if the item is NULL, but the problem is that it could switch to another thread and delete the item right after the if. How do I stop this?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QtConcurrent locking

    Well obviously you want to create a lock on the list so only one thread can change it, and release the lock when done.

    But, I guess with a little bit of redesigning it might be possible without a lock. But you need to provide more details about the list, the threads, what they contain, what they do, your algorithms etc...

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConcurrent locking

    Qt Code:
    1. void MainWindow::getThumbnail(QStandardItem *item)
    2. {
    3. if (item != NULL) {
    4. item->setData(QImage(item->data(MainWindow::ImagePathRole).toString()).scaled(64, 64), Qt::DecorationRole);
    5. }
    6. }
    7.  
    8. void MainWindow::onDirViewActivated(QModelIndex index) // if this is called again before the thumbnail functions end, it will segfault.
    9. {
    10. qDebug() << dirTreeModel->data(index).toString();
    11. QDirIterator i("/home/michael/Pictures", QDir::Files); // fix this to use the selected dir
    12. imageListModel->clear();
    13. while (i.hasNext()) {
    14. i.next();
    15. QStandardItem *item = new QStandardItem(i.fileName());
    16. item->setData(i.filePath(), ImagePathRole);
    17. QtConcurrent::run(getThumbnail, item);
    18. imageListModel->appendRow(item);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtConcurrent locking

    You cannot operate on QStandardItem objects from within threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConcurrent locking

    What to do then?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtConcurrent locking

    Return the image from the concurrent function and use QFuture interface to set it onto the item in the main thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConcurrent locking

    how do I do that when I'll call QtConcurrent::run possibly hundreds of times before one of them finishes?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtConcurrent locking

    Store QFuture objects within your model (or anywhere else you want).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConcurrent locking

    Why do QFutures contain lists (I thought that the concurrent function returns one value)?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtConcurrent locking

    There are variations of QtConcurrent calls that operate on containers and process each container element in separate threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. GUI locking problem
    By Benjamin in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2012, 04:48
  2. Replies: 1
    Last Post: 10th October 2007, 10:11
  3. Locking all threads to one processor
    By chuckshaw in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2007, 19:14
  4. MySQL, locking tables
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2006, 02:00
  5. locking the combobox
    By jayw710 in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2006, 16:12

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.