线程实例一

       当我们创建线程时,首先是从QThread派生类定义一个新的线程,然后再使用该线程时,创建该线程类的对象。

class MyThread : public QThread 
{
protected:
    void run();                         /* 重载run */
};

void MyThread::run()
{
    QTcpSocket socket;
    socket.connectToHost(hostName, portNumber);          /* 建立tcp连接 */
    exec();                                              /* 进入事件循环 */
} 

int main()
{ 
    MyThread thread;                   /* 使用新创建的thread */
    thread.start();                    /* thread会执行run(),建立tcp连接并进入事件循环,直到thread终止退出事件循环 */
    thread.wait();                     /* 等待thread退出 */
    return 0;
}
//从QThread派生类时,需要重新实现QThread的虚函数run。

线程实例二

void QThread::run () [virtual protected]

       该函数是线程的入口,当我们使用start()启动线程时,新线程就会执行run()。默认的run()函数就仅仅调用了exec()进入事件循环。(while循环取代事件循环)

class Thread : public QThread
{
    Q_OBJECT

public:
    Thread(); 
    void setMessage(const QString &message);
    void stop();

protected:
    void run();

private:
    QString messageStr;
    volatile bool stopped;
};

Thread::Thread()
{
    stopped = false;
}

void Thread::run()
{
    while (!stopped)   
    {                                            
        /* 该thread就没有用到exec()进入事件循环 */
        std::cerr << qPrintable(messageStr);
        stopped = false;
        std::cerr << std::endl;
    }
}

void Thread::stop()
{
    stopped = true;
}

线程实例三

  class Worker : public QObject
  {
      Q_OBJECT

  public slots:
      void doWork(const QString &parameter) {
          QString result;
          /* ... here is the expensive or blocking operation ... */
          emit resultReady(result);
      }

  signals:
      void resultReady(const QString &result);
  };

  class Controller : public QObject
  {
      Q_OBJECT
      QThread workerThread;
  public:
      Controller() {
          Worker *worker = new Worker;
          worker->moveToThread(&workerThread);
          connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
          connect(this, &Controller::operate, worker, &Worker::doWork);
          connect(worker, &Worker::resultReady, this, &Controller::handleResults);
          workerThread.start();
      }
      ~Controller() {
          workerThread.quit();
          workerThread.wait();
      }
  public slots:
      void handleResults(const QString &);
  signals:
      void operate(const QString &);
  };

线程实例四

  class WorkerThread : public QThread
  {
      Q_OBJECT
      void run() override {
          QString result;
          /* ... here is the expensive or blocking operation ... */
          emit resultReady(result);
      }
  signals:
      void resultReady(const QString &s);
  };

  void MyObject::startWorkInAThread()
  {
      WorkerThread *workerThread = new WorkerThread(this);
      connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
      connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
      workerThread->start();
  }

 

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐