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
165
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,999
  • 7
  • 35
  • 48
164
votes
4 answers

Are global variables thread-safe in Flask? How do I share data between requests?

In my application, the state of a common object is changed by making requests, and the response depends on the state. class SomeObj(): def __init__(self, param): self.param = param def query(self): self.param += 1 …
sayantankhan
  • 2,161
  • 2
  • 17
  • 18
156
votes
3 answers

Difference between -pthread and -lpthread while compiling

What is the difference between gcc -pthread and gcc -lpthread which is used while compiling multithreaded programs?
Vishnuraj V
  • 2,819
  • 3
  • 19
  • 23
155
votes
12 answers

What does threadsafe mean?

Recently I tried to Access a textbox from a thread (other than the UI thread) and an exception was thrown. It said something about the "code not being thread safe" and so I ended up writing a delegate (sample from MSDN helped) and calling it…
Vivek Bernard
  • 2,063
  • 3
  • 26
  • 43
145
votes
6 answers

Are non-synchronised static methods thread safe if they don't modify static class variables?

I was wondering if you have a static method that is not synchronised, but does not modify any static variables is it thread-safe? What about if the method creates local variables inside it? For example, is the following code thread-safe? public…
Sled
  • 18,541
  • 27
  • 119
  • 168
141
votes
8 answers

Is the != check thread safe?

I know that compound operations such as i++ are not thread safe as they involve multiple operations. But is checking the reference with itself a thread safe operation? a != a //is this thread-safe I tried to program this and use multiple threads…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
139
votes
5 answers

Thread Safety in Python's dictionary

I have a class which holds a dictionary class OrderBook: orders = {'Restaurant1': None, 'Restaurant2': None, 'Restaurant3': None, 'Restaurant4': None} @staticmethod def addOrder(restaurant_name,…
nmat
  • 7,430
  • 6
  • 30
  • 43
135
votes
9 answers

How can I make a JUnit test wait?

I have a JUnit test that I want to wait for a period of time synchronously. My JUnit test looks like this: @Test public void testExipres(){ SomeCacheObject sco = new SomeCacheObject(); sco.putWithExipration("foo", 1000); // WAIT FOR 2…
Kylar
  • 8,876
  • 8
  • 41
  • 75
128
votes
8 answers

Is Random class thread safe?

Is it valid to share one instance of the Random class between multiple threads? And to call nextInt(int) from multiple threads in particular?
Shcheklein
  • 5,979
  • 7
  • 44
  • 53
125
votes
3 answers

Does const mean thread-safe in C++11?

I hear that const means thread-safe in C++11. Is that true? Does that mean const is now the equivalent of Java's synchronized? Are they running out of keywords?
K-ballo
  • 80,396
  • 20
  • 159
  • 169
114
votes
7 answers

Find if the installed PHP is threadsafe or nonthreadsafe?

How do I find out whether the installed version of PHP is threadsafe or not thread safe? Please note that I'm not asking the difference between a threadsafe/non thread safe installation. I would like to find out what is installed currently.
Josh
  • 1,287
  • 3
  • 10
  • 12
112
votes
3 answers

Is SecureRandom thread safe?

Is SecureRandom thread safe? That is, after initializing it, can access to the next random number be relied on to be thread safe? Examining the source code seems to show that it is, and this bug report seems to indicate that its lack of…
Yishai
  • 90,445
  • 31
  • 189
  • 263
110
votes
8 answers

What's the best way of implementing a thread-safe Dictionary?

I was able to implement a thread-safe Dictionary in C# by deriving from IDictionary and defining a private SyncRoot object: public class SafeDictionary: IDictionary { private readonly object syncRoot = new object(); …
GP.
  • 1,293
  • 5
  • 14
  • 20
105
votes
3 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
104
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
  • 12,079
  • 22
  • 71
  • 89