Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multithreading

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
0
0
CA
hello everyone,

i'm just trying to play around with some threads before makign a mulithreaded server for some purpose....

i read somewhere that if you have a static variable in a parent thread, you can access it from a child thread. i don't know how! here is a code snippet:

Code:
#include <iostream>
#include <process.h>
#include <conio.h>

unsigned int __stdcall test(void* param)
{
  std::cout << "from within thread" << std::endl;
  std::cout << "a is: " << *(static_cast<int*>(param)) << std::endl;
  
  //std::cout<<"stint is: "<<::stint<<std::endl;
  return 0;
}

int main()
{
  std::cout<<"hi" << std::endl;
  int a=44;
   static char* stint="static char";
  _beginthreadex(0, 0, test, &a, 0, 0);
  _getch(); // Give the thread some time here...
  return 0;
}

so, what i tried to do in teh above code is declare a static char * "stint" in my main method. when i spawn off a new thread, i would like to print out stint from the new thread.. (just to learn how to access static variables from a thread.)... was wondering if someone could suggest what i could do!

also, i'm not dealing with any synchronization yet! i will do that later.. i'm just trying to learn some stuff first!

thanks a lot! really appreciate any help you can give me!
drew.
 
just a clarification.. in the code i posted, i have the line: std::cout<<"stint is: "<<::stint<<std::endl; commented out... i just did this so the program would compile.. this is the line that is causing an error!
 
Threads do not change the scope rules of the language, so in order to be able to print stint, it either needs to be a global (or static) declared before the function or passed as a parameter to the function.

To pass multiple values to a thread, put them all in a structure and pass a pointer to that structure.

> _beginthreadex(0, 0, test, &a, 0, 0);
a isn't static, so the pointer becomes invalid when main() exits. This is something to watch out for if you end up using some kind of "CreateMeAThread()" function.

--
 
hey salem,

thanks for replying. i have got it to work by declaring the variable as a global variable. but i read somewhere that statically declared variables are accessible by threads. being able to do that would make my life much easier! was i just wrongly informed or do you know of a way to do that?

thanks again for replying!
 
>statically declared variables are accessible by threads

Accessible yes, but nothing thread safe by nature.

If you're into MFC you might take a peek at faq116-5162

/Per
[sub]
www.perfnurt.se[/sub]
 
thanks for replying...

i'm not using MFC for this part of my project, but i will for other parts..

right now, i have to use _beginthreadex. do you know how i can access the statically declared variable in my main method from my thread? because once i can do that, i can start actually working on my real project!

thanks!
 
> or do you know of a way to do that?
So do
Code:
_beginthreadex(0, 0, test, stint, 0, 0);

And make sure you cast to a char* and not an int* inside your function. Then you'll have access to the static inside main().

Ignore the threads - how would give another function access to a local (albeit static) variable inside main().
Adding threads does nothing to change the scope rules.

--
 
thanks for your reply... what you said makes sense.. but i was actually hoping that i could pass multiple variables to the new thread.. i guess i got a little bit confused when i read that child threads can access static variables in the parent thread. also, i thought you might be able to access them since the parent and child threads share the same address space. i guess you can't do that!

thanks again for all the responses!
 
>what you said makes sense

Yes it does, but I think you've totally misunderstood it though...

>i guess you can't do that!

No, it is the other way around. You can do it.

Salem said:
Threads do not change the scope rules of the language
Salem said:
Adding threads does nothing to change the scope rules.

Ie whether you can access a variable, static or not, depends on the scope, not thread stuff. If your code can "see" the variable as it can with for example a global (yuk) variable, then it can access it. It has nothing to do with the code running in a certain thread.

However, if you're gonna use a variable (static or not) in multiple threads you must make sure that only one thread access it at a given time. This is where mutexes, semaphores and whatnot comes in.

/Per
[sub]
www.perfnurt.se[/sub]
 
> but i was actually hoping that i could pass multiple variables to the new thread..

Try reading my first reply again then
salem said:
To pass multiple values to a thread, put them all in a structure and pass a pointer to that structure.

--
 
hi,

perfnurt, i'm not quite sure what you mean... i do understand that the variable used in the thread should be within the thread's scope.
ok.. other than declaring a global variable, if you need to pass more than one parameter (in my case, objects of 3-4 classes that i have created- well only created one of them so far..) to a thread, how would you do it? as far as i can see, beginthreadex can only accept one parameter...
 
>beginthreadex can only accept one parameter...

Hmmm, didn't salem just say that (again)?

Salem said:
salem said:
To pass multiple values to a thread, put them all in a structure and pass a pointer to that structure.


Code:
struct ThreadParam
{
	int a;
	double b;
	int c;
	int d;
};

unsigned int __stdcall test(void* param)
{
	ThreadParam* p = static_cast<ThreadParam*>(param);
	// do your stuff...
	cout << p->a << p->b << // etc...

	delete p; p = NULL;

	return 0;
}

int main()
{
	ThreadParam* p = new ThreadParam;
	p->a = 42; 
	p->b = 3.14; 
	p->c = 666; 
	p->d = 242;
	_beginthreadex(0, 0, test, p , 0, 0);
	// To avoid conflicts where the 2 threads access the same ThreadParam
	// I "new" it and pass it to the other thread and let that one delete it.
	p = NULL;
}

/Per
[sub]
www.perfnurt.se[/sub]
 
hi,

thanks for the response.. i was typing my previous message when salem responded.. and i submitted it before reading it. i guess this is like a thread synchronization issue :p

thanks again fro the responses!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top