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
5
votes
1 answer

Thread-safe design with ARC

First of all let me quote a chapter from Apple Threading Programming Guide: Be Aware of Threats to Code Correctness When using locks and memory barriers, you should always give careful thought to their placement in your code. Even locks that…
kas-kad
  • 3,736
  • 1
  • 26
  • 45
5
votes
1 answer

What is the most efficient way to create additional threads from a thread?

Question What is the most efficient way to create additional threads from a thread? Context I am redesigning an application to be more efficient. One of the largest improvements will be running concurrent operations; however I am new to concurrent…
Robert H
  • 11,520
  • 18
  • 68
  • 110
5
votes
6 answers

Thread safety of final field

Let's say I have a JavaBean User that's updated from another thread like this: public class A { private final User user; public A(User user) { this.user = user; } public void aMethod() { Thread thread = new…
Jan Krakora
  • 2,500
  • 3
  • 25
  • 52
5
votes
6 answers

.NET - Is the Queue.Enqueue method thread safe?

Let's say that I have a module that has a Queue in it. For other entities to Enqueue, they must go through a function: public sub InsertIntoQueue(Obj) MyQueue.Enqueue(Obj) end sub If I have multiple threads running and they want to call…
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
5
votes
2 answers

Are unresettable "flags" threadsafe in C#/.NET?

(Note: I already asked this question, but the answer was specific to Java, and so I am asking the same question for C# and the .NET framework. It is NOT a duplicate.) I have been using this pattern for a while, but I only recently came to think that…
leviathanbadger
  • 1,682
  • 15
  • 23
5
votes
2 answers

Java ExecutorService: should I put a lock before to use execute?

I have a class organized as follows: public class MyClass { ExecutorService pool; public MyClass(){ pool = ... //inited by a class that implements ExecutorService } public final void submit(Runnable run){ …
mat_boy
  • 12,998
  • 22
  • 72
  • 116
5
votes
4 answers

Atomic operations in ARM strex and ldrex - can they work on I/O registers?

Suppose I'm modifying a few bits in a memory-mapped I/O register, and it's possible that another process or and ISR could be modifying other bits in the same register. Can ldrex and strex be used to protect against this? I mean, they can in…
greggo
  • 3,009
  • 2
  • 23
  • 22
5
votes
2 answers

Is GetHashCode just cargo-cult here?

HttpContext.Current.Items["ctx_" + HttpContext.Current.GetHashCode().ToString("x")] I see this exact code all ... over ... the ... place but I must be overlooking something. It's common in responses to these posts to question the appropriateness of…
shannon
  • 8,664
  • 5
  • 44
  • 74
5
votes
2 answers

Is this a correct way to implement a bounded buffer in C++

I am working on a program that deals with multiple threads accessing, depositing in, and withdrawing from a bounded buffer container. I have noticed some major problems with the threads and suspect that my buffer is partially or fundamentally…
norman
  • 5,128
  • 13
  • 44
  • 75
5
votes
6 answers

C# Get Textbox value in backgroundworker dowork event

In my windows forms application I have a textbox and backgroundworker component. In dowork event of the backgroundworker I am trying to access value of the textbox. How can i do that? I'm getting following exception in dowork event handler code when…
5
votes
1 answer

How thread-safe is my uniquing code?

I have a small value class that I create lots of instances of. Often with the same value. This class is used as a kind of identifier, so the main use is comparing instances of this class with each other (via isEqual:). To save some memory and time…
Sven
  • 22,475
  • 4
  • 52
  • 71
5
votes
6 answers

How do I analyze Java source code and ensure it is Thread safe

I am reading through java codes to ensure it is thread safe. As I understand, any local variables within the method is thread safe since it belongs to the stack memory address. Any class / instance variables is not thread safe as it belongs to the…
ilovetolearn
  • 2,006
  • 5
  • 33
  • 64
5
votes
6 answers

On the thread safety of instance variable initialization

I see this idiom of initializing instance variables quite a bit public class Test{ private Long l = 1l; private MyClass mc = new MyClass(); public Test(){} ... } But I would prefer public class Test{ private Long l; …
non sequitor
  • 18,296
  • 9
  • 45
  • 64
5
votes
1 answer

Serial communication C/C++ Linux thread safe?

My question is quite simple. Is reading and writing from and to a serial port under Linux thread-safe? Can I read and write at the same time from different threads? Is it even possible to do 2 writes simultaneously? I'm not planning on doing so but…
Silver
  • 1,075
  • 3
  • 12
  • 37
5
votes
1 answer

Cancel/abort a connection from the ThreadSafeClientConnManager connection pool

I'm using ThreadSafeClientConnManager to manage a pool of client connections, because my application has several threads, which are simultaneously connecting to a webserver. Abstract sample code: HttpClient httpClient; ClientConnectionManager…
znq
  • 44,613
  • 41
  • 116
  • 144
1 2 3
99
100