qt-thread

qt4-thread

// hellothread/hellothread.h
class HelloThread : public QThread
{
Q_OBJECT
private:
void run();
};

// hellothread/hellothread.cpp
void HelloThread::run()
{
qDebug() << "hello from worker thread " << thread()->currentThreadId();
}

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
HelloThread thread;
thread.start();
qDebug() << "hello from GUI thread " << app.thread()->currentThreadId();
thread.wait(); // do not exit before the thread is completed!
return 0;
}

等待唤醒

QWaitCondition m_wait;
QMutex m_mutex;

m_mutex.lock();
...
m_wait.wait(&m_mutex);
m_mutex.unlock();

m_wait.wakeOne();

同步

QMutex mutex;

void someMethod()
{
QMutexLocker locker(&mutex);
qDebug()<<"Hello";
qDebug()<<"World";
}

更新UI