<p>arguably thread safety is a more abstract concept that would be difficult to formally/strictly define although there are some definitions with variations in the literature depending on the specific context. </p>

<p>thread safety also fits in highly with "best practices" and "design patterns". loosely defined, a thread safe program does not have bugs related to multithreading. this definition is intentionally vague because defining thread safety exactly would almost be as difficult as defining what constitutes a "bug".</p>

<p>heres another way to approach it. generally thread count $n$ is the variable in multithreaded system. thread safety means that the code "runs the same" or "gets the same results" regardless of $n$ including $n=1$, or for both $n=1$ and $n&gt;1$.</p>

<p>here is a <a href="http://www.ibm.com/developerworks/java/library/j-jtp09263/index.html" rel="nofollow">taxonomy</a> of "degrees of thread safety" outlined by a java practitioner/authority/author that would probably be general across languages:</p>

<p>immutable, thread-safe, conditionally thread-safe, thread-compatible, and thread-hostile</p>

<p>the general trend is to create high level abstraction libaries for multithreading (see intel threadable building blocks, java concurrent library) and use the higher level object abstractions instead of lower-level language features. (eg in java this would be "synchronized" code, special thread-related keywords like "transient" etcetera).</p>

<p>there is a lot of research in this area in papers &amp; one paper I recall attempted to create and prove that a simple system built out of "pipes and filters" design pattern (similar to unix) was both thread safe and that many alternatives were likely not threadsafe, but cant locate it at the moment.</p>

CC BY-SA 3.0
2012-02-08T03:44:15.450Z
your taxonomy ( http://www.ibm.com/developerworks/java/library/j-jtp09263/index.html ) is something that I expected to find as a "short-term" answer; other answers are also usefull and interesting, but they are "long-term" ones
user1123502
CC BY-SA 3.0
2012-08-08T15:26:10.093Z
fyi the paper I may be thinking of might be on [CSP](http://en.wikipedia.org/wiki/Communicating_sequential_processes)
vzn
CC BY-SA 4.0
2020-12-25T10:18:47.577Z
I hope Santa brought you a brand new Shift key.
Andrej Bauer
CC BY-SA 3.0
2012-02-08T00:31:30.837Z
2012-02-08T00:31:30.837Z
vzn
<p>There has been a lot of work on this, usually under the heading of "memory models".  See, for example:</p>

<ul>
<li>The <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/" rel="noreferrer">Java memory model</a></li>
<li><a href="http://dl.acm.org/citation.cfm?id=1375591" rel="noreferrer">Foundations of the C++ Concurrency Memory Model</a> by Boehm and Adve</li>
<li>Lots of <a href="http://www.cl.cam.ac.uk/~pes20/weakmemory/index.html#CPP" rel="noreferrer">recent work</a> by Peter Sewell's group on C++ concurrency and hardware memory models</li>
</ul>

CC BY-SA 3.0
2012-02-07T23:51:22.447Z
unfortunately there are many ways to create non-threadsafe thread operations in java.
vzn
CC BY-SA 3.0
2012-02-08T03:54:45.283Z
The Boehm and Adve paper is dated to 2008. Does c++11 standard follow the paper? May I read the paper as an "explanation of c++11 standard concurrency model"?
user1123502
CC BY-SA 3.0
2012-02-08T04:03:35.287Z
@vzn it is interesting to look at non-threadsafe thread operations in java; it is very interesting and instructive to find out that "something obvious" is really not obvious, but wrong; do you have links?
user1123502
CC BY-SA 3.0
2012-02-08T05:45:03.490Z
there are some excellent refs on java concurrency. see eg _java concurrency in practice_ book. this ref popped up specifically looking for thread "antipatterns" [defining a catalog of programming anti-patterns for concurrent java](http://faculty.uoit.ca/bradbury/sqrg/papers/SPAQu09/SPAQu09.pdf) by bradbury et al
vzn
CC BY-SA 3.0
2012-02-07T22:32:11.727Z
2012-02-07T22:32:11.727Z
Sam Tobin-Hochstadt
<p>Wikipedia has the following definition:</p>

<blockquote>
  <p>Thread safety is a computer programming concept applicable in the
  context of multi-threaded programs. A piece of code is thread-safe if
  it only manipulates shared data structures in a manner that guarantees
  safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.[1][2] ... and so on ( <a href="http://en.wikipedia.org/wiki/Thread-safety" rel="nofollow">http://en.wikipedia.org/wiki/Thread-safety</a> )</p>
</blockquote>

<p>So, code is thread-safe if it operates in thread-safe manner! Hmm...</p>

<p>However, precise definition seems not to be simple. A naive approach is:</p>

<p>Thread-safety means that program's observable behavior does not depend on eligible thread scheduling order.</p>

<p>Still, it may be incorrect definition. Say, threads may log  messages into log file, and reodering of log messages may be OK. So program observable behavior may vary, but the program may be considered thread-safe.  </p>

<p>Probably, we need to specify allowed types of different program observable behaviors while being thread-safe.</p>

<p>Yet another complication: there may be  thread-safe (or thread-unsafe) code, not the whole program. (One may think about compositional properties of (non-)thread-safe code and so on)</p>

<p>So, the questions are: how to precisely define thread safety? Is there concise definition? What work was done to define thread safety?</p>

<p><strong>UPDATE:</strong> Thanks for all the high quality links you supplied. The links contain answers to additional question(s) that I planned to ask. Feel free to add new (appropriate) links even though I have chosen my prefered answer.</p>

CC BY-SA 3.0
2012-02-07T22:23:03.580Z
I believe [this](http://en.wikipedia.org/wiki/Consistency_model) is what you are looking for. The stronger the guarantees the harder it is to implement (maybe even impossible in the absence of some assumptions). So there are several relaxations that are easier to implement, but weaker relaxations may be more appropriate to some applications than others.
Mohammad Alaggan
CC BY-SA 3.0
2012-02-08T00:54:59.240Z
You might also want to review the literature on the relatively new field of [transactional memory](http://www.amazon.fr/Principles-Transactional-Memory-Rachid-Guerraoui/dp/1608450112/ref=wl_it_dp_o_npd?ie=UTF8&coliid=I2P84RABYVW7UM&colid=Q65J908PW37M).
Mohammad Alaggan
CC BY-SA 3.0
2012-02-08T17:23:39.543Z
Please remember that cstheory is for *theoretical* computer science. Although theoretical software engineering and formal methods questions are welcome on cstheory, [programmers.se] would be a better place to ask software engineering questions of if you are looking for practical answers.
Kaveh
CC BY-SA 3.0
2012-02-09T13:06:52.570Z
@Kaveh Sure. But let me explain. Practical aspects of a problem serve me as guides to choose "what theory I need to learn now, and what to defer for future". Is http://programmers.stackexchange.com/ a proper place to ask a question like "what papers I need to read and what papers I need to look at"? [Next, although I have chosen vzn's answer, I have voted up Sam Tobin-Hochstadt's answer.]
user1123502
CC BY-SA 3.0
2012-02-09T14:13:44.103Z
@user1123502, software engineers also read and write papers. :)
Kaveh
CC BY-SA 3.0
2012-02-09T15:19:04.463Z
@Kaveh Is my next plannig question suitable for cstheory site? It is "Prolog-like language with safe '!' operator" (that is, such an '!' operator that permitted only if it does not change semantics of a program (but it can reduce execution time). It is intended for something like a functional programming, but with logic flavor. Was there such a language? Is that a sensible idea? And so on.)
user1123502
CC BY-SA 3.0
2012-02-09T16:12:03.910Z
@Kaveh [warning: a huge doze of irony] Will my reluctance to really implement such a languge be a sufficient reason to ask question here, not at 'programmers'? ;-) Seriously, I'm new to stackoverflow, so can not estimate relative number of people interested in such a question on 'cstheory' and 'programmers'.
user1123502
CC BY-SA 3.0
2012-02-09T22:09:22.043Z
@user1123502, well, I don't think it is. (I am not an expert on PL. Generally question about PL theory are welcome on cstheory. If you want to check if something is in the scope you should ask the people working in PL theory.) ps: You can check the [FAQ] to see what we toughly mean by theoretical computer science. Theoretical doesn't mean non-practical. :)
Kaveh
CC BY-SA 3.0
2012-02-09T23:21:26.850Z
@Kaveh I shall try "programmers" site. Is it allowed to reask "programmers" question here, after, say, 1 week of question's residence at "programmers"?
user1123502
CC BY-SA 3.0
2012-02-09T23:33:22.280Z
@user1123502, yes, as long as they are in the scope of cstheory that is fine, if you don't get a satisfying answer after a week you can repost it here.
Kaveh
<reference-request><soft-question><pl.programming-languages><big-picture><terminology>
(concise?) definition of thread safety
user1123502
# (concise?) definition of thread safety

(Asked by: username_0 on 2012-02-07)

Wikipedia has the following definition:

> Thread safety is a computer programming concept applicable in the
> context of multi-threaded programs. A piece of code is thread-safe if
> it only manipulates shared data structures in a manner that guarantees
> safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.[1][2] ... and so on ( <http://en.wikipedia.org/wiki/Thread-safety> )

So, code is thread-safe if it operates in thread-safe manner! Hmm...

However, precise definition seems not to be simple. A naive approach is:

Thread-safety means that program's observable behavior does not depend on eligible thread scheduling order.

Still, it may be incorrect definition. Say, threads may log messages into log file, and reodering of log messages may be OK. So program observable behavior may vary, but the program may be considered thread-safe.

Probably, we need to specify allowed types of different program observable behaviors while being thread-safe.

Yet another complication: there may be thread-safe (or thread-unsafe) code, not the whole program. (One may think about compositional properties of (non-)thread-safe code and so on)

So, the questions are: how to precisely define thread safety? Is there concise definition? What work was done to define thread safety?

**UPDATE:** Thanks for all the high quality links you supplied. The links contain answers to additional question(s) that I planned to ask. Feel free to add new (appropriate) links even though I have chosen my prefered answer.

**Tags:** reference-request, soft-question, pl.programming-languages, big-picture, terminology
> **Comments:**
> **username_1**: I believe [this](http://en.wikipedia.org/wiki/Consistency_model) is what you are looking for. The stronger the guarantees the harder it is to implement (maybe even impossible in the absence of some assumptions). So there are several relaxations that are easier to implement, but weaker relaxations may be more appropriate to some applications than others.  
> **username_1**: You might also want to review the literature on the relatively new field of [transactional memory](http://www.amazon.fr/Principles-Transactional-Memory-Rachid-Guerraoui/dp/1608450112/ref=wl_it_dp_o_npd?ie=UTF8&coliid=I2P84RABYVW7UM&colid=Q65J908PW37M).  
> **username_2**: Please remember that cstheory is for *theoretical* computer science. Although theoretical software engineering and formal methods questions are welcome on cstheory, [programmers.se] would be a better place to ask software engineering questions of if you are looking for practical answers.  
> **username_0**: @Kaveh Sure. But let me explain. Practical aspects of a problem serve me as guides to choose "what theory I need to learn now, and what to defer for future". Is http://programmers.stackexchange.com/ a proper place to ask a question like "what papers I need to read and what papers I need to look at"? [Next, although I have chosen vzn's answer, I have voted up Sam Tobin-Hochstadt's answer.]  
> **username_2**: @user1123502, software engineers also read and write papers. :)  
> **username_0**: @Kaveh Is my next plannig question suitable for cstheory site? It is "Prolog-like language with safe '!' operator" (that is, such an '!' operator that permitted only if it does not change semantics of a program (but it can reduce execution time). It is intended for something like a functional programming, but with logic flavor. Was there such a language? Is that a sensible idea? And so on.)  
> **username_0**: @Kaveh [warning: a huge doze of irony] Will my reluctance to really implement such a languge be a sufficient reason to ask question here, not at 'programmers'? ;-) Seriously, I'm new to stackoverflow, so can not estimate relative number of people interested in such a question on 'cstheory' and 'programmers'.  
> **username_2**: @user1123502, well, I don't think it is. (I am not an expert on PL. Generally question about PL theory are welcome on cstheory. If you want to check if something is in the scope you should ask the people working in PL theory.) ps: You can check the [FAQ] to see what we toughly mean by theoretical computer science. Theoretical doesn't mean non-practical. :)  
> **username_0**: @Kaveh I shall try "programmers" site. Is it allowed to reask "programmers" question here, after, say, 1 week of question's residence at "programmers"?  
> **username_2**: @user1123502, yes, as long as they are in the scope of cstheory that is fine, if you don't get a satisfying answer after a week you can repost it here.  

---

## 2 answers

### Answer by username_3 on 2012-02-08. Score: 0 | Accepted answer

arguably thread safety is a more abstract concept that would be difficult to formally/strictly define although there are some definitions with variations in the literature depending on the specific context.

thread safety also fits in highly with "best practices" and "design patterns". loosely defined, a thread safe program does not have bugs related to multithreading. this definition is intentionally vague because defining thread safety exactly would almost be as difficult as defining what constitutes a "bug".

heres another way to approach it. generally thread count $n$ is the variable in multithreaded system. thread safety means that the code "runs the same" or "gets the same results" regardless of $n$ including $n=1$, or for both $n=1$ and $n>1$.

here is a [taxonomy](http://www.ibm.com/developerworks/java/library/j-jtp09263/index.html) of "degrees of thread safety" outlined by a java practitioner/authority/author that would probably be general across languages:

immutable, thread-safe, conditionally thread-safe, thread-compatible, and thread-hostile

the general trend is to create high level abstraction libaries for multithreading (see intel threadable building blocks, java concurrent library) and use the higher level object abstractions instead of lower-level language features. (eg in java this would be "synchronized" code, special thread-related keywords like "transient" etcetera).

there is a lot of research in this area in papers & one paper I recall attempted to create and prove that a simple system built out of "pipes and filters" design pattern (similar to unix) was both thread safe and that many alternatives were likely not threadsafe, but cant locate it at the moment.

> **Comments:**
> **username_0**: your taxonomy ( http://www.ibm.com/developerworks/java/library/j-jtp09263/index.html ) is something that I expected to find as a "short-term" answer; other answers are also usefull and interesting, but they are "long-term" ones  
> **username_3**: fyi the paper I may be thinking of might be on [CSP](http://en.wikipedia.org/wiki/Communicating_sequential_processes)  
> **username_4**: I hope Santa brought you a brand new Shift key.  

---

### Answer by username_5 on 2012-02-07. Score: 6

There has been a lot of work on this, usually under the heading of "memory models". See, for example:

* The [Java memory model](http://www.cs.umd.edu/~pugh/java/memoryModel/)
* [Foundations of the C++ Concurrency Memory Model](http://dl.acm.org/citation.cfm?id=1375591) by Boehm and Adve
* Lots of [recent work](http://www.cl.cam.ac.uk/~pes20/weakmemory/index.html#CPP) by Peter Sewell's group on C++ concurrency and hardware memory models

> **Comments:**
> **username_3**: unfortunately there are many ways to create non-threadsafe thread operations in java.  
> **username_0**: The Boehm and Adve paper is dated to 2008. Does c++11 standard follow the paper? May I read the paper as an "explanation of c++11 standard concurrency model"?  
> **username_0**: @vzn it is interesting to look at non-threadsafe thread operations in java; it is very interesting and instructive to find out that "something obvious" is really not obvious, but wrong; do you have links?  
> **username_3**: there are some excellent refs on java concurrency. see eg _java concurrency in practice_ book. this ref popped up specifically looking for thread "antipatterns" [defining a catalog of programming anti-patterns for concurrent java](http://faculty.uoit.ca/bradbury/sqrg/papers/SPAQu09/SPAQu09.pdf) by bradbury et al  

---

