noobie thread question
Mar 24, 2010 at 6:36am UTC
I have classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
class FirstClass
{
public :
FirstClass();
SecondClass *second;
void LaunchSecond(){
this ->second = new SecondClass(this );
this ->second->testnumber = 92;
boost::thread(&SecondClass::ThreadFunc, this ->second);
while (1)
{
outputmutex->lock();
cout<<"Main thread: " <<this ->second->testnumber<<endl;
outputmutex->unlock();
}
};
};
class SecondClass
{
FirstClass *first;
public :
int testNumber;
SecondClass(FirstClass *x){ this ->first = x };
void ThreadFunc(){
while (1)
{
outputmutex->lock();
cout<<"Second thread: " <<this ->testNumber<<endl;
outputmutex->unlock();
}
};
}
The thread launches without error, but cout's the wrong number. The main thread shows 92, so this isn't a mutex issue, but rather the thread seems to be launching with a bad copy of SecondClass instead of a reference to (this->second)
Any idea on what's causing this or how I can fix it? I've tried just about every combination of boost::bind and boost::ref I can think of.
Thanks in advance :)
Mar 24, 2010 at 7:12am UTC
Are testNumber and testnumber different variables or is that just a typo?
Mar 24, 2010 at 8:02am UTC
What is the output you are getting ?
Mar 24, 2010 at 3:44pm UTC
It's a typo, they're the same
I'm getting
Main thread: 92
Main thread: 92
Second thread: -12748
Main thread: 92
Second thread: -12748
Main thread: 92
Second thread: -12748
Topic archived. No new replies allowed.