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.