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

  • Home
  • SEARCH
  • 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 4044578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:16:29+00:00 2026-05-20T13:16:29+00:00

I have two threads One and Two. defined by their respective classes in the

  • 0

I have two threads One and Two. defined by their respective classes in the header file.I want to start the second thread when the first thread is started. creating and starting the second thread in the constructor of the first produced unexpected result.
my header file “header.h”

#ifndef HEADER
#define HEADER
#include <QtGui>
class One:public QThread
{
public:
    One();
    void run();

};

class Two:public QThread
{
public:
    Two();
    void run();
};
#endif

my class file “main.cpp”

#include "header.h"
#include<iostream>
using namespace std;

One::One()
{
/* the output just hangs at thread two and does not get to thread one run */
Two b;
b.start();
b.wait();

}
void One::run()
{
    cout<<"One run\n";
    int i=0;
    for(;;)
    {

        i++;
        cout<<"+++ "<<i<<endl;
        if(i==10)
            break;
        sleep(3);
    }
}

Two::Two()
{

}
void Two::run()
{

    cout<<"Two run\n";
    int i=0;
    for(;;)
    {

        i--;
        cout<<"----- "<<i<<endl;
        sleep(3);
    }
}
int main(int argc,char* argv[])
{
    One a;
   // Two b;
    a.start();
   // b.start();
   a.wait();
   // b.wait();
    return(0);

}

This is the working code of how i expect the output to run.

Edit: changed the code so that now
both the threads are properly
independent

How do i start the second thread along with the first thread, without explicitly calling two in main i.e .

int main(int argc,char* argv[])
{
     One a;
    Two b;
    a.start();
    b.start();
    a.wait();
    b.wait();
   return(0);
}

The invoking and handling of thread two should be done by thread one..

  • 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-20T13:16:30+00:00Added an answer on May 20, 2026 at 1:16 pm

    I believe you’ve perhaps misunderstood some threading concepts. It sounds like you want to start two threads, then from one thread context make a function call to another thread context, which is not how threads work.

    When you start these two threads, they execute independently of each other. They can share access to data, but you can’t intertwine their execution the way you seem to want to. If you want one thread to perform something on request of another, you have two decide two things:

    • Which mechanism to use for signaling the request from one thread to the other
    • Whether the requesting thread should stop and wait for a “receipt” or just happily continue while the other thread does what it was asked to.

    A very simple (and not really safe, this is just illustrating the logics) mechanism for doing this would be using two bools to signal a request, e.g:

    Thread one starts                         | Thread two starts
    Thread one works                          | Thread two loops checking a `bool DoSomething;`
    Thread one sets bool DoSomething          | 
    Thread one loops waiting for DidSomething | Thread two beeps
                                              | Thread two sets DidSomething
    Thread one continues working              | 
    

    The thing to notice is that there’s no “calls” in between thread contexts. The two threads execute simultaneously, and communicate by using data (the two bools).

    In real world multithreading, you have to worry about simultaneous access to data. What would e.g. happen if two threads, at the same time on a dual-core machine, tried to append data to the same list. Both threads may see the same “end of list pointer”, both would create a new entry, both would update the “end of list pointer”. But one of the changes will overwrite the other, and in best case you’d have some lost data and a memory leak, in worst case you’d have a crash.

    This is where you use a “mutual exclusion” mechanism: each thread will, before accessing such a shared resource as the list, get hold of a mutex. A mutex is constructed in such a way that only one thread at a time can “own” it. If thread one happens to acquire the mutex first, it’ll proceed to do its list update, and then let go of the mutex. In the meantime, the other threads attempt to acquire the mutex will simply sit there, the Mutex::acquire() call will not return until thread one is done with the mutex. If both threads behave nicely, and acquire the mutex before accessing the shared list, the simultaneous update described above will not happen, and the list will be perfectly valid after both threads have updated it.

    In response to comments:

    You cannot start the two threads simultaneously. The closest approximation would be to create and start Two from within One::run:

    void One::run()
    {
        Two b;
        b.start();
        cout<<"One run\n";
        int i=0;
        for(;;)
        {
    
            i++;
            cout<<"+++ "<<i<<endl;
            if(i==10)
                break;
            sleep(3);
        }
        b.wait();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two threads in an Android application, one is the view thread, and
I have two classes one and two . Both run threads. class Two is
I have two threads, one needs to poll a bunch of separate static resources
I have two threads, one updating an int and one reading it. This is
I have big process running. It spawns two threads. I want to debug those
So here I am again, unsure what to do.. I have two threads. One
I need to have two way communication between threads in Tcl and all I
I have some code that will be accessed from two threads: class Timer{ public:
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
I have two arrays of System.Data.DataRow objects which I want to compare. The rows

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.