166
General / Pass data to a thread function
« on: October 02, 2010, 04:19:34 am »
Hi, I have a simple question about passing a variable to a thread function that I think it's easy to answer.
I was following this tutorial:
http://www.sfml-dev.org/tutorials/1.6/system-threads.php
And I read the part that explains my question, however I'm not having success when I try to pass a variable to the thread function.
I have the variable int n declared on the main, where n's value is 5.
Now I want to pass this value to a threaded function.
I tried the following:
But it's not outputting n correctly.
So, what could be wrong here?
Thanks in advance
I was following this tutorial:
http://www.sfml-dev.org/tutorials/1.6/system-threads.php
And I read the part that explains my question, however I'm not having success when I try to pass a variable to the thread function.
I have the variable int n declared on the main, where n's value is 5.
Now I want to pass this value to a threaded function.
I tried the following:
Code: [Select]
void print_Console(void *UserData)
{
int* n = static_cast<int*>(UserData);
cout << "n=" << *n << endl;
}
int main()
{
int n=2; //EDITED AT Sat Oct 02, 2010 11:50 am
Thread th1(&print_Console, &n);
th1.Launch();
}
But it's not outputting n correctly.
So, what could be wrong here?
Thanks in advance