Functions when multithreading

I have a function and inside this function, I store some variables.

Let's say the first thread runs the function, some values get stored in to "the function variables" and then the first thread stops while the second thread takes over.

The second thread starts and run the same function, but this thread stores other values in to "the function variables", while the first thread still haven't finished.


Now the first thread takes over again and is going to finish the function. Before the finish, the thread has to use "the function variables" in a calc.


So I wonder, is these function calls "unique and protected from other calls", or has the second thread changed "the function variables" for the first thread in this case?


Best regards
Volang

All your dynamic local variables will be unique (and maintained) between different threads.

Each thread maintains a separate context (usually in the form of a stack), and it's within this context that function call hierarchy, parameters and local variables are stored.
1
2
3
4
5
void foo ( int var ) {
  int baz = var; // no problem here
  static int count = 0;  // static makes this NOT thread safe!
  // code
}

Hey man.

Thanks for your reply. Simple and clear.

Topic archived. No new replies allowed.