C
cppaddict
What is the proper way to pass information to new threads?
I am working with some legacy code --- a Runnable object which passes
info to its new threads by setting member variables before starting
them. The run() method uses these member variable values to determine
its behavior.
The problem is that, when many threads are started at once, a member
variable can change before a thread has a chance to execute. When the
thread does execute, it will execute using the wrong parameters
(member variables) because those parameters have changed value in
between the time the thread was started and actually had a chance to
execute. Thus unintended behavior replaces the intended behavior.
My question is: What is the right way to handle a situation like this?
How can you get each new thread its own copy of what are essentially
parameter values?
Thanks for any thoughts,
cpp
I am working with some legacy code --- a Runnable object which passes
info to its new threads by setting member variables before starting
them. The run() method uses these member variable values to determine
its behavior.
The problem is that, when many threads are started at once, a member
variable can change before a thread has a chance to execute. When the
thread does execute, it will execute using the wrong parameters
(member variables) because those parameters have changed value in
between the time the thread was started and actually had a chance to
execute. Thus unintended behavior replaces the intended behavior.
My question is: What is the right way to handle a situation like this?
How can you get each new thread its own copy of what are essentially
parameter values?
Thanks for any thoughts,
cpp