Questions tagged [thread-safety]

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads. A code may be thread safe, conditionally safe (mutual exclusion required) or unsafe (can only be safely used by one thread).

Thread Safety

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads. A code may be thread-safe, conditionally-safe (mutual exclusion required), or unsafe (can only be safely used by one thread).

Main approaches to thread safety include re-entrancy, thread-local storage, mutual exclusion (locking), atomic operations, and immutable objects.

Resources

8652 questions
95
votes
8 answers

Why are local variables thread safe in Java

I was reading multi-threading in Java and I come across this Local variables are thread safe in Java. Since then I have been thinking How/Why local variables are thread safe. Can somebody please let me know.
Anand
  • 18,380
  • 43
  • 116
  • 188
93
votes
4 answers

Is a HashMap thread-safe for different keys?

If I have two multiple threads accessing a HashMap, but guarantee that they'll never be accessing the same key at the same time, could that still lead to a race condition?
agentofuser
  • 8,291
  • 10
  • 48
  • 81
93
votes
3 answers

how to know what is NOT thread-safe in ruby?

starting from Rails 4, everything would have to run in threaded environment by default. What this means is all of the code we write AND ALL the gems we use are required to be threadsafe so, I have few questions on this: what is NOT thread-safe in…
CuriousMind
  • 31,669
  • 24
  • 84
  • 127
90
votes
3 answers

Threadsafe vs re-entrant

Recently, I asked a question, with title as "Is malloc thread safe?", and inside that I asked, "Is malloc re-entrant?" I was under the impression that all re-entrant are thread-safe. Is this assumption wrong?
Alphaneo
  • 10,931
  • 20
  • 65
  • 87
88
votes
3 answers

lock(new object()) -- Cargo cult or some crazy "language special case"?

I'm reviewing some code written by a consultant, and while dozens of red flags have already popped up, I can't wrap my head around the following snippet: private void foo() { if (InvokeRequired) { lock (new object()) { …
user434817
81
votes
14 answers

Android - Best and safe way to stop thread

I want to know which is the best way to stop a thread in Android. I know I can use AsyncTask instead of it and that there is a cancel() method. I have to use Threads in my situation. Here is how I'm using Thread: Runnable runnable = new Runnable()…
Android-Droid
  • 13,649
  • 37
  • 107
  • 184
81
votes
4 answers

Safety of Thread.current[] usage in rails

I keep getting conflicting opinions on the practice of storing information in the Thread.current hash (e.g., the current_user, the current subdomain, etc.). The technique has been proposed as a way to simplify later processing within the model layer…
Giuseppe
  • 4,776
  • 4
  • 36
  • 35
81
votes
3 answers

Is it safe to call a synchronized method from another synchronized method?

If a synchronized method calls another synchronized method, is it thread safe? void synchronized method1() { method2() } void synchronized method2() { }
user705414
  • 17,800
  • 36
  • 105
  • 151
79
votes
9 answers

efficient thread-safe singleton in C++

The usual pattern for a singleton class is something like static Foo &getInst() { static Foo *inst = NULL; if(inst == NULL) inst = new Foo(...); return *inst; } However, it's my understanding that this solution is not thread-safe,…
user168715
  • 5,201
  • 1
  • 29
  • 40
76
votes
12 answers

Is malloc thread-safe?

Is the malloc() function re-entrant?
Alphaneo
  • 10,931
  • 20
  • 65
  • 87
76
votes
3 answers

How to create a task (TPL) running a STA thread?

Using Thread is pretty straightforward Thread thread = new Thread(MethodWhichRequiresSTA); thread.SetApartmentState(ApartmentState.STA); How to accomplish the same using Tasks in a WPF application? Here is some code: Task.Factory.StartNew …
Michel Triana
  • 2,386
  • 1
  • 20
  • 31
72
votes
7 answers

How to ensure thread safety of utility static method?

Is there any general way or rules exits by which we can ensure the thread safety of static methods specifically used in various Utility classes of any applications. Here I want to specifically point out the thread safety of Web Applications. It is…
Tapas Bose
  • 25,780
  • 71
  • 202
  • 317
70
votes
4 answers

Is RestTemplate thread safe?

Is a Spring RestTemplate thread-safe? That is Is a RestTemplate a Strategy object that multiple connections can safely share. or Is a RestTemplate a connection object (like a data-base connection), which can not be shared while in use, and requires…
Raedwald
  • 40,290
  • 35
  • 127
  • 207
68
votes
3 answers

Multithreading program stuck in optimized mode but runs normally in -O0

I wrote a simple multithreading programs as follows: static bool finished = false; int func() { size_t i = 0; while (!finished) ++i; return i; } int main() { auto result=std::async(std::launch::async, func); …
sz ppeter
  • 1,514
  • 1
  • 5
  • 14
68
votes
4 answers

how to make an application thread safe?

I thought thread safe, in particular, means it must satisfy the need for multiple threads to access the same shared data. But, it seems this definition is not enough. Can anyone please list out the things to be done or taken care of to make an…
ashmish2
  • 2,755
  • 6
  • 35
  • 53