问题
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:
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();
}
来源:https://stackoverflow.com/questions/5230444/c-qthread-starting-2-threads-concurrently