Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wonder, would it not be possible for an IDE to detect any shared mutable objects that are used in multiple threads?

You could flag types as either [ThreadSafe] or [Immutable] using attributes in .NET for example, and then those variables would never cause warnings, but any other variable that is used inside of a method invoked via Thread would be highlighted as "possibly unsafe" unless all assignments occur inside of a lock or something like that.

I know it wouldn't be perfect because the compiler simply can't reason about all the possible scenarios (humans barely can), but hints like these would be quite valuable, no?

Is this even feasible?

share|improve this question
Thread safety is much more complicated than that. So my hunch is: No, it's about as useful as asking the compiler to find violations of GUI design guidelines ;-) – delnan Mar 7 '13 at 20:18
This task may be easier with certain more restrictive languages where the compilers could have an easier time analyzing possible code paths. I do know that some papers exist on this subject. Still, I think the answer in the near-future is no. – MrFox Mar 7 '13 at 20:24
@MrFox: So you're saying it would be very difficult to build this type of functionality into existing languages such as C#, but it may be more feasible in a language designed with this goal in mind? – mpen Mar 7 '13 at 20:36
In fact, there are numerous research languages geared towards making concurrency easier to do correctly. They often take more radical approaches than just allowing detection of potential issues, many take new approaches to concurrency entirely (i.e. not using threads/locks/shared memory explicitly). That's not even getting into attempts of automatic parallelization. – delnan Mar 7 '13 at 21:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.