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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables in multithread enviorment

Status
Not open for further replies.

xpto

Technical User
Jul 10, 2001
13
PT
I got this problem:

I need to preserve the value of a variable between the executions of the same function. I know this can be done with the "static" keyword. But the problem is that this function is called by different threads. In this case the use of the keyword "static" implies that the variable will be the same in all the different threads, and one change made by one of the threads will affect the valued stored by the other threads.

How can i solve this problem ???

Tanx in advance...
 
You can't.

You have to improvise something like an array of static variabiles (as many as threads) and using an identificator passed to the thread as parameter use them.

Anyway I recomend you to use the InterlockedIncrement and InterlockedDecrement functions.

Hope this helps,

s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
make a thread class. This is a pseudocode:
class CThread
{
variables there
public:
exec(){}
static threadfunc(...void*..)
{
(CThread*)x->exec();
return ...;
}
declare some method Run()
{
call threadfunc(..(void*)this...)
}
};

CThread x;
x.Run();//call the thread John Fill
1c.bmp


ivfmd@mail.md
 
What I've done in the past is create a struct to hold all per-thread variables, then pass it in to the thread routine via it's PVOID parameter. If I update anything in this struct, I use the Interlocked* functions like BurtonI said, or occasionally I'll use a CriticalSection. If I read an int from the structure, I do it directly and store it in a function-local variable the first time. If it's a complicated variable in the structure (like a string or another struct), I'll use a locking function (like above) to copy it to a local variable.

The rest of the code will use the local variable, not the structure, in order to prevent changes outside the thread from returning different results at different times in the thread.

Chip H.
 
What I've forgotten to add is, instead of static variables in thread functions you will use class variables. This is much more reliable. But if you have problems with sinconisation, just use CriticalSectiona or/and mutexes. Mutex you can use to sincronize threads on different processes. John Fill
1c.bmp


ivfmd@mail.md
 
Thank you all for your most valueable help. I'm happy to say that i've already found a solution for my problem !

In each thread i created a non-static variable and i passed it's address as an argument to the function that was shared between all the threads. In this way, i'm able to store the "static" value for each function and restore it between calls to that function. This solves my initial problem.

Once more, thank you all for your help.
 
Most multi-threaded operating systems have a facility to solve the problem mentioned above. It is called thread-local storage.

For Windows, Richter has a whole chapter on the topic.

Hope this helps,

Brudnakm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top