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 7594029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:18:50+00:00 2026-05-30T21:18:50+00:00

I am learning how to run objects on different threads. I started with this

  • 0

I am learning how to run objects on different threads. I started with this simple exercise to run objects on different threads and communicate between them. Now, I have trouble terminating the program. How to safely destroy the objects running in different threads when main exits? If I put a wait on threads before main for the completion of threads, the program hangs at wait(). If I don’t put a wait, threads are never executed and threads are deleted immediately as main exits.

    --------testthread.h------------------
    #ifndef TESTTHREAD_H
    #define TESTTHREAD_H

   #include <QThread>
   #include <QApplication>
   #include <string>
   #include <iostream>

   using namespace std;
   class testThread : public QObject
   {
     Q_OBJECT
     public:
     testThread(const string&, QThread*);
    ~testThread();
private:
    QThread* thread;
    string _threadName;
signals:
    void requestCalculateSquare(int);
public slots:
    void calculateSquare(int);
    void start();

  };

   #endif // TESTTHREAD_H}

   -----testthread.cpp---------------------
   #include "testthread.h"
   #include <iostream>
   //#include "moc_testthread.cpp"

   testThread::~testThread()
   {}

   testThread::testThread(const string& threadName, QThread* thread):QObject()
   {
      _threadName = threadName;
       moveToThread(thread);
       connect(thread, SIGNAL(started()),this,SLOT(start()));
   }

   void testThread::calculateSquare(int i)
   {
      cout<<_threadName<<" "<<i*i;
   }

   void testThread::start()
    {
       for(int i=0;i<10;++i)
       {
         emit requestCalculateSquare(i);
       }
         emit finished();
    }

     --------main.cpp--------------------------
    #include <iostream>
    #include <QtCore/QCoreApplication>
    #include <string> 
    #include "testthread.h"

    using namespace std;

    int main(int argc, char** argv)
    {
       QApplication app(argc,argv);
       string name1("Thread1");
       string name2("Thread2");
       QThread* thread1 = new QThread;
       QThread* thread2 = new QThread;
       testThread*  test1 = new testThread(name1, thread1);
       testThread* test2 = new testThread(name2, thread2);

         QObject::connect(test1,SIGNAL(requestCalculateSquare(int)),test2,SLOT(calculateSquare(int)));
QObject::connect(test2,SIGNAL(requestCalculateSquare(int)),test1,SLOT(calculateSquare(int)));
    QObject::connect(test1,SIGNAL(finished()),test2, SLOT(deleteLater()));
    QObject::connect(test1,SIGNAL(finished()),thread2, SLOT(quit()));
    QObject::connect(test2,SIGNAL(finished()),test1, SLOT(deleteLater()));
    QObject::connect(test2,SIGNAL(finished()),thread1, SLOT(quit()));

thread1->start();
thread2->start();

thread1->wait();
thread2->wait();

cout << "Hello World!" << endl;
return 0;
//return app.exec();
}
  • 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-05-30T21:18:51+00:00Added an answer on May 30, 2026 at 9:18 pm

    You’re doing some funky things.

    1) Each QThread is having start() called twice – first in the testThread constructor, then again in main(). The second call won’t do anything, since is is already running.

    2) When you start the QThread in the testThread constructor, your loop will execute and emit the signals before they are connected – it makes me think you don’t actually want to start the thread in the constructor, and instead leave it unstarted until later in main().

    3) QThread::quit() will cause the thread’s event loop to return – this is what you’re missing. connect a “done” type signal from testThread to the quit slot, and then QThread::wait() will behave as you expect, with the calls returning once the loops are completed.

    4) I know it’s just a little test program, but you’re leaking memory since you aren’t calling delete on your new objects. You actually don’t need to use new at all here, everything can be allocated on the stack – generally a better idea.

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

Sidebar

Related Questions

I have just started learning CXF. I made a maven archetype project through eclipse.
I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition
I have been recently learning some OOP PHP and seem to have run into
I am learning synchronized blocks with locks.I want to know the difference between this
I'm learning C# coming from C++ and have run into a wall. I have
Have run into jquery question while learning ajax. $('div_hello'); --> answers an 'Object' document.getElementById('div_hello');
I need to run Linux-Apache-PHP-MySQL application (Moodle e-learning platform) for a large number of
On my journey to learning F#, I've run into a problem I cant solve.
I'm learning iPhone programming from Erica Sadun's The iPhone Developer's Cookbook. When I run
I'm learning how unit testing is done in Rails, and I've run into a

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.