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
68
votes
12 answers

Where do I get a thread-safe CollectionView?

When updating a collection of business objects on a background thread I get this error message: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread. Ok, that makes sense.…
Jonathan Allen
  • 63,625
  • 65
  • 234
  • 426
68
votes
9 answers

Unit test for thread safe-ness?

I've written a class and many unit test, but I did not make it thread safe. Now, I want to make the class thread safe, but to prove it and use TDD, I want to write some failing unit tests before I start refactoring. Any good way to do this? My…
TheSean
  • 4,438
  • 7
  • 37
  • 49
66
votes
5 answers

Is java.sql.Connection thread safe?

To rephrase the question: should I avoid sharing instances of classes which implement java.sql.Connection between different threads?
Boris Pavlović
  • 58,387
  • 26
  • 115
  • 142
63
votes
4 answers

How to correctly read an Interlocked.Increment'ed int field?

Suppose I have a non-volatile int field, and a thread which Interlocked.Increments it. Can another thread safely read this directly, or does the read also need to be interlocked? I previously thought that I had to use an interlocked read to…
Roman Starkov
  • 52,420
  • 33
  • 225
  • 300
63
votes
3 answers

Is DbContext thread safe?

I was wondering if the DbContext class is thread safe, I am assuming it's not, as I am currently executing paralell threads that access the DbContext in my application and I am getting a host of locking exceptions and other things that look like…
jcvandan
  • 13,404
  • 16
  • 60
  • 97
63
votes
2 answers

What do each memory_order mean?

I read a chapter and I didn't like it much. I'm still unclear what the differences is between each memory order. This is my current speculation which I understood after reading the much more simple…
user34537
62
votes
6 answers

How to stop a thread created by implementing runnable interface?

I have created class by implementing runnable interface and then created many threads(nearly 10) in some other class of my project.How to stop some of those threads?
svkvvenky
  • 992
  • 1
  • 14
  • 21
61
votes
6 answers

Is a volatile int in Java thread-safe?

Is a volatile int in Java thread-safe? That is, can it be safely read from and written to without locking?
shawn
  • 3,713
  • 6
  • 32
  • 53
61
votes
4 answers

Is Cipher thread-safe?

Quite simply, can one instance of javax.crypto.Cipher (e.g. Cipher.getInstance("RSA")) be used from multiple threads, or do I need to stick multiple of them in a ThreadLocal (in my case)?
Bart van Heukelom
  • 40,403
  • 57
  • 174
  • 291
61
votes
3 answers

Is Joda-Time DateTimeFormatter class thread safe?

Is the Joda-Time DateTimeFormatter class thread safe? Once I get an instance from DateTimeFormat.forPattern, can its various parse methods be called by multiple threads? DateTimeFormatter's Javadocs makes no mention of thread safety.
Steve Kuo
  • 58,491
  • 75
  • 189
  • 247
60
votes
12 answers

How to implement thread-safe lazy initialization?

What are some recommended approaches to achieving thread-safe lazy initialization? For instance, // Not thread-safe public Foo getInstance(){ if(INSTANCE == null){ INSTANCE = new Foo(); } return INSTANCE; }
mre
  • 40,416
  • 33
  • 117
  • 162
60
votes
1 answer

What is the difference between SynchronizedCollection and the other concurrent collections?

How does SynchronizedCollection and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection being a…
Batrickparry
  • 1,329
  • 2
  • 13
  • 14
60
votes
4 answers

Are function static variables thread-safe in GCC?

In the example code void foo() { static Bar b; ... } compiled with GCC is it guaranteed that b will be created and initialized in a thread-safe manner ? In gcc's man page, found the -fno-threadsafe-statics command line option: Do not emit the…
CsTamas
  • 3,803
  • 5
  • 28
  • 34
59
votes
2 answers

difference between standard's atomic bool and atomic flag

I wasn't aware of the std::atomic variables but was aware about the std::mutex (weird right!) provided by the standard; however one thing caught my eye: there are two seemingly-same (to me) atomic types provided by the standard, listed…
hg_git
  • 2,234
  • 5
  • 20
  • 42
58
votes
3 answers

Is PHP thread-safe?

Is PHP (as of 5.2) thread-safe on Linux/UNIX? Would it be possible to use it with Apache Worker-MPM or Event-MPM? The facts I gathered so far are inconclusive: Default binaries included in most distributions have ZTS disabled, so I'm aware, that…
vartec
  • 118,560
  • 34
  • 206
  • 238