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
57
votes
2 answers

What is thread Safe in java?

Possible Duplicate: What does threadsafe mean? I am very confused that any class is Thread safe. I am understanding that, if any class is thread safe then it has some specific on its methods(as synchronized). Is it right or wrong? Please help me…
Anuj
  • 591
  • 1
  • 5
  • 4
57
votes
4 answers

Are Python built-in containers thread-safe?

I would like to know if the Python built-in containers (list, vector, set...) are thread-safe? Or do I need to implement a locking/unlocking environment for my shared variable?
Phong
  • 6,092
  • 3
  • 30
  • 56
56
votes
9 answers

C# thread safety with get/set

This is a detail question for C#. Suppose I've got a class with an object, and that object is protected by a lock: Object mLock = new Object(); MyObject property; public MyObject MyProperty { get { return property; } set { …
mmr
  • 14,271
  • 28
  • 89
  • 142
55
votes
5 answers

Thread safe logging class implementation

Would the following be the correct way to implement a fairly straightforward thread-safe logging class? I know that I never explicitly close the TextWriter, would that be a problem? When I initially used the TextWriter.Synchronized method, it did…
Sean Thoman
  • 7,209
  • 6
  • 53
  • 102
55
votes
4 answers

Are C# arrays thread safe?

In particular Create a function to take an array and an index as parameters. Create a n element array. Create a n count loop. Inside the loop on a new thread assign a new instance of the object to the array using the indexer passed in. I know…
Gary
  • 1,767
  • 1
  • 13
  • 22
54
votes
6 answers

Is HttpSession thread safe, are set/get Attribute thread safe operations?

Also, does the object that is being set have to be thread safe in order to guarantee that we know what the state of the object stored in session is known. Also, I was reading on the web that some suggest using: synchronized(session) { …
Berlin Brown
  • 10,806
  • 35
  • 129
  • 193
53
votes
4 answers

SharedPreferences and Thread Safety

Looking at the SharedPreferences docs it says: "Note: currently this class does not support use across multiple processes. This will be added later." So in and of itself it doesn't appear to be Thread Safe. However, what kind of guarantees…
cottonBallPaws
  • 20,217
  • 37
  • 119
  • 169
53
votes
2 answers

Is the Session object from Python's Requests library thread safe?

Python's popular Requests library is said to be thread-safe on its home page, but no further details are given. If I call requests.session(), can I then safely pass this object to multiple threads like so: session = requests.session() for i in…
DJG
  • 5,903
  • 3
  • 27
  • 50
50
votes
2 answers

Initializing ThreadStatic field still causes NullReferenceException

I've written myself a multi-threaded random generator public static class MyRandGen { private static Random GlobalRandom = new Random(); [ThreadStatic] private static Random ThreadRandom = new Random(SeedInitializer()); private…
Tarec
  • 3,124
  • 3
  • 26
  • 42
50
votes
8 answers

Is the += operator thread-safe in Python?

I want to create a non-thread-safe chunk of code for experimentation, and those are the functions that 2 threads are going to call. c = 0 def increment(): c += 1 def decrement(): c -= 1 Is this code thread safe? If not, may I understand why…
nubela
  • 14,600
  • 22
  • 68
  • 115
50
votes
21 answers

Synchronizing on String objects in Java

I have a webapp that I am in the middle of doing some load/performance testing on, particularily on a feature where we expect a few hundred users to be accessing the same page and hitting refresh about every 10 seconds on this page. One area of…
matt b
  • 132,562
  • 64
  • 267
  • 334
48
votes
6 answers

Why we need Thread.MemoryBarrier()?

In "C# 4 in a Nutshell", the author shows that this class can write 0 sometimes without MemoryBarrier, though I can't reproduce in my Core2Duo: public class Foo { int _answer; bool _complete; public void A() { _answer = 123; …
Felipe Fujiy Pessoto
  • 6,492
  • 8
  • 40
  • 71
48
votes
4 answers

How to use std::atomic<> effectively for non-primitive types?

The definitions for std::atomic<> seem to show its obvious usefulness for primitive or perhaps POD-types. When would you actually use it for classes? When should you avoid using it for classes?
kfmfe04
  • 14,193
  • 11
  • 67
  • 132
48
votes
6 answers

Are Generators Threadsafe?

I have a multithreaded program where I create a generator function and then pass it to new threads. I want it to be shared/global in nature so each thread can get the next value from the generator. Is it safe to use a generator like this, or will I…
Corey Goldberg
  • 53,391
  • 24
  • 118
  • 137
47
votes
4 answers

What is "Linearizability"?

Can anyone out there help me understand what Linearizability is? I need an explanation that is simple and easy to understand. I'm reading The Art of Multiprocessor Programming by Maruice Herilihy and Nir Shavit and am trying to understand Chapter…
unnknown
  • 1,395
  • 2
  • 16
  • 33