I decided to create a sample application where the local static QMutex is used. Mutex is used in the print function. If I don't use it, text is printed in a wrong order. It seems to work fine, except it, perhaps, violates some rule you mentioned. I didn't notice any bugs or any other problems so far, do you think this code is incorrect? Honestly, I don't know the rule you are talking about. Where can I read it?

Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <QThread>
  3. #include <QMetaType>
  4. #include <QMutex>
  5. #include <iostream>
  6.  
  7. void print(const QString &message)
  8. {
  9. using namespace std;
  10.  
  11. static QMutex mutex;
  12. mutex.lock(); // if we run it without mutex, lines may be mixed
  13. cout << QThread::currentThreadId() << " " << qPrintable(message) << endl;
  14. mutex.unlock();
  15. }
  16.  
  17. class Printer: public QObject
  18. {
  19. Q_OBJECT
  20.  
  21. public slots:
  22. void startPrinting()
  23. {
  24. for (int i = 0; i < 1000; ++i)
  25. {
  26. // perform some processing...
  27. // ...
  28.  
  29. // print something on the screen/to the log/whatever
  30. print(QString("i = %1").arg(i));
  31. }
  32.  
  33. emit finished();
  34. }
  35.  
  36. signals:
  37. void finished();
  38. };
  39.  
  40. //Q_DECLARE_METATYPE(Printer*)
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44. QCoreApplication a(argc, argv);
  45.  
  46. QThread *t1 = new QThread;
  47. Printer *printer1 = new Printer;
  48. printer1->moveToThread(t1);
  49.  
  50. QThread *t2 = new QThread;
  51. Printer *printer2 = new Printer;
  52. printer2->moveToThread(t2);
  53.  
  54. QObject::connect(t1, SIGNAL(started()), printer1, SLOT(startPrinting()));
  55. QObject::connect(printer1, SIGNAL(finished()), t1, SLOT(quit()));
  56. QObject::connect(t1, SIGNAL(finished()), printer1, SLOT(deleteLater()));
  57. QObject::connect(t1, SIGNAL(finished()), t1, SLOT(deleteLater()));
  58.  
  59. QObject::connect(t2, SIGNAL(started()), printer2, SLOT(startPrinting()));
  60. QObject::connect(printer2, SIGNAL(finished()), t2, SLOT(quit()));
  61. QObject::connect(t2, SIGNAL(finished()), t2, SLOT(deleteLater()));
  62. QObject::connect(t2, SIGNAL(finished()), printer2, SLOT(deleteLater()));
  63.  
  64. t1->start();
  65. t2->start();
  66.  
  67. return a.exec();
  68. }
  69.  
  70. #include "main.moc"
To copy to clipboard, switch view to plain text mode