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

Is this a valid, lazy, thread-safe Singleton implementation for C#?

I implemented a Singleton pattern like this: public sealed class MyClass { ... public static MyClass Instance { get { return SingletonHolder.instance; } } ... static class SingletonHolder { public static…
Matthew
  • 28,056
  • 26
  • 104
  • 170
5
votes
1 answer

Is the connect() call thread safe in Qt?

I have two QObjects A and B living in separate QThreads. A will emit a signal while B has a matching slot. I want to use connect() to connect A's signal to B's slot. So the question is, is the connect() call thread safe? Does it matter in which of…
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
5
votes
2 answers

Ramifications of CheckForIllegalCrossThreadCalls=false

I recently updated an application from VS2003 to VS2008 and I knew I would be dealing with a host of "Cross-thread operation not valid: Control 'myControl' accessed from a thread other than the thread it was created on" I am handling this in what I…
Ron Skufca
  • 2,028
  • 5
  • 27
  • 34
5
votes
2 answers

is newKieSession thread-safe?

We used Drools kieSessions in our project. Many threads can create new kieSession. Sometimes thread can hang while creating session. Hence the question: Firstly is kieContainer.newKieSession thread-safe operation? Can the reason of hanging be a…
Eugene Stepanenkov
  • 896
  • 16
  • 34
5
votes
3 answers

WPF - Task.Run(() => window.ShowDialog) fails

I have to implement busy indication and progress reporting. The constraint is, that I have to use the provided Control Library, which offers a Window for progress reporting. The following code works fine, but does not block the UI, which in some…
Yannik
  • 709
  • 1
  • 7
  • 11
5
votes
3 answers

Why are static methods in .Net framework classes always thread-safe?

I have noticed the following statement in most places of .Net framework documentation. Question: What is the secret to this? I don't think a static class is always thread-safe. My question relates to standard classes that are available in .Net…
Sunil
  • 20,653
  • 28
  • 112
  • 197
5
votes
4 answers

Can subsequent writes in .NET be reordered by the runtime or the processor?

I have immutable objects whose hashcode I wish to calculate lazily. I've implemented private bool _HasHashCode = false; private int _HashCode; public override int GetHashCode() { if (_HasHashCode) return _HashCode; long hashCode; …
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
5
votes
1 answer

Entity Framework and Thread safety of ObjectContext

Suppose that we have an ObjectContext (via Entity Framework EDMX) with some entities. Entities fully loaded from DataBase from one single thread. Only after the entities was loaded we start some threads which will only read data from entities and…
5
votes
1 answer

Thread Safe In Value Receiver In Go

type MyMap struct { data map[int]int } func (m Mymap)foo(){ //insert or read from m.data } ... go func f (m *Mymap){ for { //insert into m.data } }() ... Var m Mymap m.foo() When I call m.foo(),…
frank.lin
  • 1,614
  • 17
  • 26
5
votes
3 answers

Thread safety in java web application?

What does someone mean when I am asked that whether my web application is thread safe or not , considering that I have not used Multiple threads in my webapplication.
Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
5
votes
1 answer

Producer/Consumer pattern with a batched producer

I'm attempting to implement a fairly simple Producer/Consumer style application with multiple producers and one consumer. Research has led me onto the BlockingCollection which is useful and allowed me to implement a long-running consumer task as…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
5
votes
0 answers

ActiveRecord transactions & thread safety

My question is about AR transactions feature. Is it thread safe? Is it safe to use both ActiveRecord::Base.transaction do ... and Account.transaction do ... statements? I plan to use Puma as web server, and Sidekiq for background…
5
votes
1 answer

Java - Android : Thread being called (run) twice

I would like some help regarding Java - Android MultiThreading While learning to develop my app in a multi-threading way in order to take advantage of the ever-growing multi-core devices market share (most devices are quad core now, some even…
Mackovich
  • 3,319
  • 6
  • 35
  • 73
5
votes
1 answer

Return locked resource from class with automatic unlocking

I would like to have a class member function which returns a pointer to a resource. The resource should be locked and unlocked automatically. I think of creating a non-copyable object which handles the locking. Do you think the following is a good…
Danvil
  • 22,240
  • 19
  • 65
  • 88
5
votes
3 answers

Is AtomicInteger.incrementAndGet thread safe?

For example, if I run this code from multiple threads and each thread for many times, will there be potentially a data race? public boolean swap(int i, int j) { if (value.get(i) <= 0 || value.get(j) >= maxval) { return false; } …
peps
  • 75
  • 1
  • 1
  • 6