3 Replies - 8351 Views - Last Post: 29 July 2012 - 12:43 AM

#1 JohnBoyMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-February 12

set variables from inside a thread

Posted 29 March 2012 - 02:30 AM

Hello all. I am working with threads at the moment in my program. The thread is simple and looks like this.

void ThreadProc(void){

				 
};


Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) );
oThread->Start();



I have another simplar thread like this but its more complex. I try to set variables in it like this, for example.


int y;
void ThreadProc(void){
y=9;
				 
};


Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) );
oThread->Start();
cout<<y;




y will be 0!

This is veryyy simple stuff, but if i cout<<y; from outside of the ThreadProc it will be 0 always. I have tried a few other things, i dont ever remeber having this problem, i have been coding for 5 years,so am i nuts? Or should i use a delegate or something? thanks

Is This A Good Question/Topic? 0
  • +

Replies To: set variables from inside a thread

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: set variables from inside a thread

Posted 29 March 2012 - 02:47 AM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: set variables from inside a thread

Posted 29 March 2012 - 04:12 AM

You know, computers are very fast... :P

Put a cout in your ThreadProc as well and see what you get:
void ThreadProc() {
cout<<"ThreadProc entered" << endl;
y=9;
cout << y << endl;
};



My first guess is that your print is running before your thread completely loaded up.
Was This Post Helpful? 0
  • +
  • -

#4 Cenron  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 13
  • Joined: 23-May 12

Re: set variables from inside a thread

Posted 29 July 2012 - 12:43 AM

View PostJohnBoyMan, on 29 March 2012 - 02:30 AM, said:

Hello all. I am working with threads at the moment in my program. The thread is simple and looks like this.

void ThreadProc(void){

				 
};


Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) );
oThread->Start();



I have another simplar thread like this but its more complex. I try to set variables in it like this, for example.


int y;
void ThreadProc(void){
y=9;
				 
};


Thread^ oThread = gcnew Thread( gcnew ThreadStart( this,&qualifiedlist::ThreadProc ) );
oThread->Start();
cout<<y;




y will be 0!

This is veryyy simple stuff, but if i cout<<y; from outside of the ThreadProc it will be 0 always. I have tried a few other things, i dont ever remeber having this problem, i have been coding for 5 years,so am i nuts? Or should i use a delegate or something? thanks



Use the static keyword to declare the int as a global variable,
 static int y = 0;



The other way you can do it is declare it as a pointer.
so it can survive the scope of the function, I like this method better because then your not polluting the global namespace.

unsigned int __stdcall ThreadProc( void *param )
{
  int *y = reinterpret_cast<int *>( param );
  if( y != NULL )
    (*y) = 9;
}


int main()
{
  int y = 0; // Or int *y = new int;

  // Pass the address of the variable as a reference. 
  CreateThread( &ThreadProc, reinterpret_cast<void *>(&y) );
  std::cout << y << std::endl; // Should be 9.
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1