Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to wait for all threads in main thread?
QtWS25 Last Chance

[SOLVED] How to wait for all threads in main thread?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    unmanner
    wrote on last edited by
    #1

    I just post the most interesting places from my application, could you please help me and describe how I should wait for all threads?

    main.c:
    @int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    bdConnection = new RmSqlConnection();
    
    GetMail* getMail = new GetMail(bdConnection);
    getMail->startThreads();
    
    return a.exec();
    

    }
    @

    getthread.c
    @QThread* GetMail::addThread(int userId)
    {
    GetMailTask* getTask = new GetMailTask(sysRootPath, archPath);
    QThread* thread = new QThread;

    getTask->moveToThread(thread);
    
    connect(thread,  SIGNAL(started()),  getTask, SLOT(process()));
    connect(getTask, SIGNAL(finished()), thread,  SLOT(quit()));
    connect(this,    SIGNAL(stopAll()),  getTask, SLOT(stop()));
    connect(getTask, SIGNAL(finished()), getTask, SLOT(deleteLater()));
    connect(thread,  SIGNAL(finished()), thread,  SLOT(deleteLater()));
    connect(getTask, SIGNAL(finished()), this, SLOT(startNewThread()));
    
    return thread;
    

    }

    void GetMail::startThreads() {

    int i = 1;
    QThread* thread;
    
    /* Prepare threads */
    while(userEntry_r.next()) {
    
        if (NULL != (thread = addThread(userId))) {
            threadList.append(thread);
        }
    }
    
    /* Run threads */
    while ( (!threadList.isEmpty()) && (i <= maxNumberOfThreads) ) {
        threadList.takeFirst()->start();
        i++;
    }
    

    }

    void GetMail::startNewThread() {

    if (threadList.isEmpty()) {
        return;
    }
    
    threadList.takeFirst()->start();
    

    }
    @

    getmailtask.c:
    @
    void GetMailTask::process()
    {
    ...Do something...
    emit finished();
    }@

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unmanner
      wrote on last edited by
      #2

      So, may be the following change may give some positive effect?

      @void GetMail::startNewThread() {

      if (threadList.isEmpty()) {
      
          if (maxNumberOfThreads-- == 0) {
              /* All started thread are finished, lets close application */
              QApplication::exit(0);
          }
      
          return;
      }
      
      threadList.takeFirst()->start();
      

      }@

      1 Reply Last reply
      0
      • C Offline
        C Offline
        code_fodder
        wrote on last edited by
        #3

        Hi unmanner, yes I was just about to suggest that you need a way of tracking your threads. You can either have a list to all the threads you start and delete them out of the list when you get the finished() signal.

        Or you can have a counter and wait for it to reach zero. But it looks like you are on the right track there...

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved