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

9311 questions
1169
votes
16 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
  • 37,019
  • 22
  • 105
  • 153
930
votes
5 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..
  • 10,925
  • 5
  • 20
  • 8
635
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
  • 21,141
  • 40
  • 118
  • 183
266
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,667
  • 2
  • 15
  • 8
260
votes
8 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
  • 29,597
  • 21
  • 65
  • 64
257
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
  • 45,608
  • 10
  • 63
  • 75
252
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
  • 90,700
  • 113
  • 281
  • 364
237
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
  • 5,015
  • 5
  • 30
  • 40
227
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
  • 12,170
  • 26
  • 77
  • 106
212
votes
8 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
  • 2,133
  • 2
  • 12
  • 4
209
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,720
  • 8
  • 31
  • 47
206
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,990
  • 11
  • 35
  • 48
180
votes
15 answers

Thread-safe List property

I want an implementation of List as a property which can be used thread-safely without any doubt. Something like this: private List _list; private List MyT { get { // return a copy of _list; } set { _list = value; } } It seems…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
179
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
  • 33,387
  • 34
  • 113
  • 219
171
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,591
  • 7
  • 28
  • 34
1
2 3
99 100