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
1006
votes
17 answers

Collection was modified; enumeration operation may not execute

I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur. Collection was modified; enumeration operation may not execute Below is the code. This is a WCF server in a Windows service. The method…
cdonner
  • 34,608
  • 21
  • 96
  • 146
842
votes
4 answers

What is thread safe or non-thread safe in PHP?

I saw different binaries for PHP, like non-thread or thread safe? What does this mean? What is the difference between these packages?
O..
  • 9,925
  • 5
  • 18
  • 8
583
votes
9 answers

What does a lock statement do under the hood?

I see that for using objects which are not thread safe we wrap the code with a lock like this: private static readonly Object obj = new Object(); lock (obj) { // thread unsafe code } So, what happens when multiple threads access the same code…
NLV
  • 19,811
  • 35
  • 115
  • 178
254
votes
3 answers

Use cases for RxJava schedulers

In RxJava there are 5 different schedulers to choose from: immediate(): Creates and returns a Scheduler that executes work immediately on the current thread. trampoline(): Creates and returns a Scheduler that queues work on the current thread to…
bcorso
  • 40,568
  • 8
  • 56
  • 73
252
votes
9 answers

Why is Java's SimpleDateFormat not thread-safe?

Please tell with a code example why is SimpleDateFormat not threadsafe. What is the problem in this class? Is The problem with format function of SimpleDateFormat? Please give a code which demonstrates this fault in class. FastDateFormat is…
Vivek Sharma
  • 2,527
  • 2
  • 12
  • 7
219
votes
2 answers

Is local static variable initialization thread-safe in C++11?

I know this is an often asked question, but as there are so many variants, I'd like to re-state it, and hopefully have an answer reflecting the current state. Something like Logger& g_logger() { static Logger lg; return lg; } Is the…
Ralph Zhang
  • 4,617
  • 4
  • 25
  • 36
218
votes
8 answers

What exactly is a reentrant function?

Most of the times, the definition of reentrance is quoted from Wikipedia: A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely…
Lazer
  • 79,569
  • 109
  • 264
  • 349
205
votes
11 answers

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized…
wuntee
  • 11,330
  • 26
  • 75
  • 105
201
votes
7 answers

Queue.Queue vs. collections.deque

I need a queue which multiple threads can put stuff into, and multiple threads may read from. Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be…
miracle2k
  • 22,364
  • 17
  • 60
  • 62
190
votes
9 answers

Automating the InvokeRequired code pattern

I have become painfully aware of just how often one needs to write the following code pattern in event-driven GUI code, where private void DoGUISwitch() { // cruisin for a bruisin' through exception city object1.Visible = true; …
Tom Corelis
  • 4,632
  • 9
  • 33
  • 44
180
votes
5 answers

Concurrent HashSet in .NET Framework?

I have the following class. class Test{ public HashSet Data = new HashSet(); } I need to change the field "Data" from different threads, so I would like some opinions on my current thread-safe implementation. class Test{ …
kukab
  • 1,813
  • 2
  • 10
  • 4
171
votes
5 answers

C# Thread safe fast(est) counter

What is the way to obtain a thread safe counter in C# with best possible performance? This is as simple as it gets: public static long GetNextValue() { long result; lock (LOCK) { result = COUNTER++; } return…
JohnDoDo
  • 4,180
  • 8
  • 27
  • 43
168
votes
4 answers

What Makes a Method Thread-safe? What are the rules?

Are there overall rules/guidelines for what makes a method thread-safe? I understand that there are probably a million one-off situations, but what about in general? Is it this simple? If a method only accesses local variables, it's thread…
Bob Horn
  • 30,755
  • 28
  • 102
  • 197
160
votes
5 answers

Is iterating ConcurrentHashMap values thread safe?

In javadoc for ConcurrentHashMap is the following: Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update…
Palo
  • 10,049
  • 7
  • 24
  • 32
151
votes
4 answers

Is it OK to use Gson instance as a static field in a model bean (reuse)?

Here's the model I implemented: public class LoginSession { private static final Gson gson = new Gson(); private String id; private String name; private long timestamp; public LoginSession(String id, String name) { …
philipjkim
  • 3,619
  • 6
  • 30
  • 45
1
2 3
99 100