patternMinor
Is there a term for the common semantics of global variables, static local variables, and fields?
Viewed 0 times
localglobalthesemanticsfieldstermforvariablesandthere
Problem
I'm working on a graph representation of software for a static analysis project, and I'm finding a distinction that I have no name for between different types of variables.
On one side of the division, we have local variables and parameters, which are not subject to modification by other threads during the execution of a method. For these variables, static single assignment form is valid.
On the other side of the division, we have global variables, fields, and C/C++ static local variables, which can be modified by other threads, meaning static single assignment form is not necessarily valid, as the variable may be modified by a separate thread during the execution of the method.
Is there any existing term for this division?
On one side of the division, we have local variables and parameters, which are not subject to modification by other threads during the execution of a method. For these variables, static single assignment form is valid.
On the other side of the division, we have global variables, fields, and C/C++ static local variables, which can be modified by other threads, meaning static single assignment form is not necessarily valid, as the variable may be modified by a separate thread during the execution of the method.
Is there any existing term for this division?
Solution
One standard term is "shared mutable state".
Let's unpack that. "State" is any place to store data: e.g., a variable or object. "Mutable" means that the data can be modified. "Shared" means it is shared among multiple threads. Thus, shared mutable state is state that is visible to multiple threads and can be modified by multiple threads.
Usually, immutable state is not a concern: even if it can be read by multiple threads, no one can write to it, so there is no potential for data races or concurrence problems.
Usually, non-shared state is not a concurrency concern: there's only a single thread that can read or write it, so we don't have to worry about concurrency -- merely single-threaded-correctness.
Note that this also helps clarify how we should think about a case that wasn't mentioned in your original question. What about state that can only be written by a single thread, but can be viewed by multiple threads? That's shared mutable state. And that's exactly the right classification, because such state can cause concurrency problems: if you're not careful, it can cause data races and other issues, so it's only right that it be lumped together with other kinds of shared mutable state.
(For instance, in your question you write that "local variables... are not subject to modification by other threads during the execution of a method". This is true, but it doesn't go far enough. The relevant fact about local variables is that they can't be modified or read by other threads. Also, this highlights that not all local variables qualify: for instance, if you take the address of the local variable and pass that pointer to another thread, the local variable now becomes shared mutable state and is a concurrency problem.)
Let's unpack that. "State" is any place to store data: e.g., a variable or object. "Mutable" means that the data can be modified. "Shared" means it is shared among multiple threads. Thus, shared mutable state is state that is visible to multiple threads and can be modified by multiple threads.
Usually, immutable state is not a concern: even if it can be read by multiple threads, no one can write to it, so there is no potential for data races or concurrence problems.
Usually, non-shared state is not a concurrency concern: there's only a single thread that can read or write it, so we don't have to worry about concurrency -- merely single-threaded-correctness.
Note that this also helps clarify how we should think about a case that wasn't mentioned in your original question. What about state that can only be written by a single thread, but can be viewed by multiple threads? That's shared mutable state. And that's exactly the right classification, because such state can cause concurrency problems: if you're not careful, it can cause data races and other issues, so it's only right that it be lumped together with other kinds of shared mutable state.
(For instance, in your question you write that "local variables... are not subject to modification by other threads during the execution of a method". This is true, but it doesn't go far enough. The relevant fact about local variables is that they can't be modified or read by other threads. Also, this highlights that not all local variables qualify: for instance, if you take the address of the local variable and pass that pointer to another thread, the local variable now becomes shared mutable state and is a concurrency problem.)
Context
StackExchange Computer Science Q#24277, answer score: 2
Revisions (0)
No revisions yet.