Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:21:48+00:00 2026-06-07T03:21:48+00:00

Scenario Lets say, I have a procedure called parallelRun . It would take a

  • 0

Scenario

Lets say, I have a procedure called parallelRun. It would take a list of workers, each having a getWorkAmount():int, a run() method, a finished() signal and a cancel() slot:

void parallelRun( std::vector< Worker* > workers );

Its implementation should:

1. Open a QPogressDialog:

unsigned int totalWorkAmount = 0;
for( auto it = workers.begin(); it != workers.end(); ++it )
{
    totalWorkAmount += ( **it ).getWorkAmount();
}

LoadUI ui( 0, totalWorkAmount, this );

with

class LoadUI : public QObject
{
    Q_OBJECT

public:

    LoadUI( int min, int max, QWidget* modalParent )
        : totalProgres( 0 )
        , progressDlg( "Working", "Abort", min, max, modalParent )
    {
        connect( &progressDlg, SIGNAL( canceled() ), this, SLOT( cancel() ) );

        progressDlg.setWindowModality( Qt::WindowModal );
        progressDlg.show();
    }

    bool wasCanceled() const
    {
        return progressDlg.wasCanceled();
    }

public slots:

    void progress( int amount )
    {
        totalProgres += amount;

        progressDlg.setValue( totalProgres );
        progressDlg.update();

        QApplication::processEvents();
    }

signals:

    void canceled();

private slots:

    void cancel()
    {
        emit canceled();
    }

private:

    int totalProgres;
    QProgressDialog progressDlg;
}

2. Create one thread for each worker

std::vector< std::unique_ptr< QThread > > threads;
for( auto it = workers.begin(); it != workers.end(); ++it )
{
    std::unique_ptr< QThread > thread( new QThread() );

    Worker* const worker = *it;
    worker->moveToThread( thread.get() );

    QObject::connect( worker, SIGNAL( finished() ), thread.get(), SLOT( quit() ) );
    QObject::connect( &ui, SIGNAL( canceled() ), worker, SLOT( cancel() ) );
    QObject::connect( *it, SIGNAL( progressed( int ) ), &ui, SLOT( progress( int ) ) );

    thread->start( priority );

    threads.push_back( std::move( thread ) );
}

3. Run them simultaneously

for( auto it = workers.begin(); it != workers.end(); ++it )
{
    QMetaObject::invokeMethod( *it, "run", Qt::QueuedConnection );
}

load() is run when the user clicks an UI-button.

Problem

How am I supposed to extend this code, if I want to make parallelRun block until all workers are finished, without freezing the QProgressDialog?

Deliberations

Using a barrier

I tried adding the following code at the end of the parallelRun routine:

QApplication::processEvents();
for( auto it = threads.begin(); it != threads.end(); ++it )
{
    ( **it ).wait();
}

The impact of this few lines of extra-code is, that LoadUI::progress is never entered, since the GUI-thread is asleep and therefore it’s event loop isn’t processed: In Qt, signals are delivered to slots by posting them to the event loop of the thread, associated to the object the slot belongs to. This is why the progressed signal of a worker is never delivered.

I think, the appropriate solution would be to run QApplication::processEvents() within the GUI-thread anytime a progressed signal is emitted by a worker. On the other hand, I guess this cannot be done, since the GUI-thread is asleep.

Another possible solution

Another possibility would be to use an active waiting-like solution:

for( auto it = threads.begin(); it != threads.end(); ++it )
{
    while( ( **it ).isRunning() )
    {
        QApplication::processEvents();
    }
}
for( auto it = threads.begin(); it != threads.end(); ++it )
{
    ( **it ).wait();
}

This also requires adding the following line of code right after thread->start( priority );:

while( !thread->isRunning() );

I don’t think that this is a nice solution, but at least it works. How can this be done without the drawbacks of active waiting?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:21:52+00:00Added an answer on June 7, 2026 at 3:21 am

    You could use the threads’ finished() signals to wait for them all to finish in the main GUI loop instead of using QApplication::processEvents. The progress dialog modality will ensure that only that dialog window is active until it is explicitly closed.

    class WorkerManager : public QObject {
        Q_OBJECT
    private:
        // to be able to access the threads and ui, they are defined as a members
        std::vector<std::unique_ptr<QThread> > threads;
        LoadUI *ui;
    
        int finishedThreadCount;
    public:
        WorkerManager() 
            : finishedThreadCount(0)
        {
            // Open the QProgressDialog
            ...
            // Create and start the threads
            ...
            // Connect the finished() signal of each thread 
            // to the slot onThreadFinished
            for( auto it = threads.begin(); it != threads.end(); ++it )  {
                QObject::connect(
                    it->get(), SIGNAL(finished()), 
                    this, SLOT(onThreadFinished()) );
            }
        }
    
    private slots:
        void onThreadFinished() {
             ++finishedThreadCount;
    
             if(finishedThreadCount == threads.size()) 
             {
                  // clean up the threads if necessary
                  // close the dialog
                  // and eventually destroy the object this itself
             }
        }
    };
    

    Or you can run a nested QEventLoop to wait for the threads to finish synchronously while still keeping the GUI responsive:

    // Open the QProgressDialog
    ...
    // Create and start the threads
    ...
    // Create and run a local event loop,
    // which will be interrupted each time a thread finishes
    QEventLoop loop;
    for( auto it = threads.begin(); it != threads.end(); ++it )  
    {
        QObject::connect(
            it->get(), SIGNAL(finished()), 
            &loop, SLOT(quit()) );
    }  
    for(int i = 0, threadCount = threads.size(); i < threadCount; ++i) 
        loop.exec();
    

    If the progress reach the maximum only when the work is completely done, you can use progressDlg->exec() instead of a QEventLoop which will block until the maximum is reached or until the user clicks on the “Cancel” button.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario I have a page lets say login.aspx having a button called
Lets take a static html page as a scenario, lets say you have a
I am using backbone.js here is my scenario Lets say I have a view
So lets say I have the following scenario. http://website.com:8080 and http://website.com:8080/demo Is there any
Maybe it's not worth worrying about in this scenario, but lets say you have
To simplify the scenario, let's say we have a list of People with FirstName
Here's my scenario: Let's say I have a stored procedure in which I need
I have following scenario: Lets say we have two different webparts operating on the
The scenario is the following: Lets say I have this application App that depends
To Set up the scenario, Lets say I have 100000 rows in the table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.