私のコードは以下の通り:
int main(int argc, char* argv[])
{
MyThread myThread1;
MyThread myThread2;
myThread1.start();
myThread2.start();
qDebug("Hello World");
myThread1.wait();
qDebug("myThread1 is finished...");
myThread2.wait();
qDebug("myThread2 is finished...");
return 0;
}
>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
};
>
void MyThread::run()
{
QMutex mutex();
int x = 10000;
mutex.lock();
while(x != 0) {
sleep(1);
qDebug("%d, x = %d ", thread()->currentThreadId(), x);
x--;
}
mutex.unlock();
}
結果は次のとおりです:
Hello World
5516, x = 10000
6060, x = 10000
5516, x = 9999
6060, x = 9999
5516, x = 9998
6060, x = 9998
5516, x = 9997
6060, x = 9997
...
...
私は結果が欲しいです:
xxxx: 10000
xxxx: 9999
xxxx: 9998
xxxx: 9997
...
...
xxxx: 1
yyyy: 10000
yyyy: 9999
...
...
yyyy: 1
why? where is my fault ? And how to use QMutex.. Thank you ...