Results 1 to 4 of 4

Thread: New to QMutex

  1. #1
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default New to QMutex

    Hello there,

    I'm new to the whole Multithreaded programming and I'm having issues.

    functions.h
    Qt Code:
    1. namespace Functions
    2. {
    3. bool existsInDB(int id);
    4. }
    To copy to clipboard, switch view to plain text mode 
    functions.cpp
    Qt Code:
    1. #include "functions.h"
    2. #include <QMutex>
    3. #include [...]
    4.  
    5. QMutex mutex(QMutex::Recursive);
    6.  
    7. bool Functions::existsInDB(int id)
    8. {
    9. static QList<int> cache;
    10.  
    11. QMutexLocker lock(&mutex);
    12.  
    13. if(cached.contains(id))
    14. return true;
    15. else
    16. {
    17. cached.append(id);
    18.  
    19. QSqlQuery query;
    20. query.prepare("SELECT COUNT(id) FROM customers WHERE id=?;");
    21. query.addBindValue(id);
    22. query.exec();
    23. query.next();
    24.  
    25. return (query.value(0).toInt() > 0)
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Now I have a subclass of QThread:
    Qt Code:
    1. void myThread::run()
    2. {
    3. bool exists = Functions::existsInDB(data->id);
    4. if(exists) {...}
    5. }
    To copy to clipboard, switch view to plain text mode 

    As you can see it calls existsInDB(). Now in the main-thread (GUI-Thread) there a sometimes also calls to existsInDB() which causes the following errors to occur in the console (can't remember the exact words):
    QSqlQuery::value not positioned on a valid row
    If I remove the existsInDB() call in the GUI thread the error disappears. If I remove the existsInDB() call in myThread the error also disappears. So I guess the problem is that both threads are trying to access that function at the same time or something but I can't wrap my head around that whole QMutex story and how to make it work? Can someone point me in the right direction?

    Thank you.

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

    Default Re: New to QMutex

    You can't use the same database connection from threads other than that where it was created. The design is incorrect - each thread needs its own database connection (and of course the mutex won't be needed anymore).
    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.


  3. #3
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: New to QMutex

    Ahh, thank you wysota. I didn't even know each thread needs its own database connection.
    But I don't quite understand how I am supposed to rewrite my code. The function is used by both threads (they share the data in static QList<int> cache;).
    And I'm having a little issue understanding - the call to Function::existsInDB() "lives" in the myThread-thread because it was called inside myThread::run()?
    How can I rewrite my function so that it works correctly? Passing the QSqlDatabase-pointer on each function call doesn't seem like such a good option.

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

    Default Re: New to QMutex

    Each thread should have its own connection stored in its private storage and when this function is called, it need to use the connection associated with the thread that has called the function. You can "name" threads and store the names as values in a map that is keyed with QThread::currentThread(). You can also "name" connections with the names you have given to threads so that you can retrieve the connection name based on the result of currentThread().
    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. The following user says thank you to wysota for this useful post:

    durbrak (13th March 2009)

Similar Threads

  1. single variable and QMutex
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 9th December 2008, 08:45
  2. threads synchronization and Qmutex
    By jiboon in forum Newbie
    Replies: 2
    Last Post: 3rd May 2008, 17:10
  3. QMutex and QDataStream
    By babu198649 in forum Newbie
    Replies: 15
    Last Post: 12th April 2008, 12:25
  4. QMutex is not working in release mode
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2007, 13:32
  5. Replies: 3
    Last Post: 12th October 2006, 21:48

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.