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

lock vs (try_lock, sleep, repeat) performance

I am updating some code and I came across several mutexs that were using the lines: while (!mutex_.try_lock()) sleep_for(milliseconds(1)); instead of just locking the mutex straight away: mutex_.lock(); Is there any performance difference…
user3758015
  • 119
  • 1
  • 9
5
votes
2 answers

What to use instead of Interlocked.Equals

I have some legacy code that uses Interlocked.Equals to compare values. The values may be two bools or it may compare an array of structs to null. Resharper complains about Interlocked.Equals saying "access to a static member of a type via a…
Bill W
  • 1,428
  • 1
  • 20
  • 29
5
votes
3 answers

How to ensure a block of code is executed only once in a multithreaded environment?

I have a non singleton actor that is creating an object that I want created only only once. How is this achieved? Can an actor be singleton? If yes, how? class NonSingletonActor extends UntypedActor { public static onReceive(Object arg) throws…
user_mda
  • 18,148
  • 27
  • 82
  • 145
5
votes
1 answer

Implementing thread safe caching of search results

How the search results caching works When a user enters a query to search for: The query is split into an array of tokens A unique hash is created for this array of tokens (order tokens alphabetically then MD5). This is the unique identity of the…
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
5
votes
2 answers

Are operations with the internal pointer of shared_ptr atomic?

Is it safe to copy and reset shared_ptr at the same time? Namely consider the following code // Main thread (before creating any other threads) shared_ptr a(new A(1)); // Thread 1 shared_ptr a_copy = a; // Thread 2 a.reset(new(A(2)); where…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
5
votes
2 answers

What is non threadsafe about a simple property get set in c#?

I have read plenty of discussions and examples of how to make a property threadsafe. There is one on the page for this threadsafe wrapper class The example given is this: internal class MyThreadSafeCass { // *** Lock *** private object…
bornfromanegg
  • 2,826
  • 5
  • 24
  • 40
5
votes
2 answers

Is I18n.with_locale threadsafe?

I have created a feature that publish a news with the language of the page's creator. Here is the code that create the news : def add_news locale = creator.language.blank? ? I18n.locale : creator.language I18n.with_locale(locale) do title =…
elhostis
  • 1,067
  • 14
  • 32
5
votes
2 answers

Thread safe mersenne twister

Looking for a thread safe random generator I found a mersenne twister generator class that the author says if thread safe: http://www.umiacs.umd.edu/~yangcj/mtrnd.html But after studying the code I cannot see were it is safe thread. There are no…
Horacio
  • 2,727
  • 5
  • 26
  • 29
5
votes
2 answers

Multiple SQLite database instances open at the same time on different Threads (QT)

Is there any problem on using many open connections at the same time from different threads? From what I've read it's thread safe by default, but, can this be hurting performance rather than improving it?
Stephen H. Anderson
  • 978
  • 4
  • 18
  • 45
5
votes
6 answers

Does partial thread-safety make a Java class thread-safe?

I came across the example below of a Java class which was claimed to be thread-safe. Could anyone please explain how it could be thread-safe? I can clearly see that the last method in the class is not being guarded against concurrent access of any…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
5
votes
3 answers

ConcurrentHashMap changes visible to all the threads?

I have a CHM defined as below. I am calling setDataProcess method from a single background thread whenever there is any update. And I am calling getDataMapping from multiple reader threads always. private static final ConcurrentHashMap
john
  • 11,311
  • 40
  • 131
  • 251
5
votes
3 answers

collect a synchronized arraylist from streams in java 8

List result = map.entrySet() .stream() .map(Map.Entry::getValue) .flatMap(x -> x.stream()) .collect(Collectors.toCollection(ArrayList::new)); The above code…
Manu Joy
  • 1,739
  • 6
  • 18
  • 30
5
votes
5 answers

Resetting a field lazy-loaded with the double-check idiom

Consider the "double-check idiom for lazy initialization of instance fields": // Item 71 in Effective Java copied from this interview with Bloch. private volatile FieldType field; FieldType getField() { FieldType result = field; if (result…
les2
  • 14,093
  • 16
  • 59
  • 76
5
votes
1 answer

notify listener inside or outside inner synchronization

I am struggling with a decision. I am writing a thread-safe library/API. Listeners can be registered, so the client is notified when something interesting happens. Which of the two implementations is most common? class MyModule { protected…
Jary Zeels
  • 51
  • 2
5
votes
1 answer

JavaEE Web JAX-RS: can i use instance variables inside it's class?

I'm looking for thread-safe Servlet alternative and I've found JAX-RS technology. So can i use instance variables inside it's class like this (is it thread safe): @Path("helloworld") public class HelloWorldResource { private String msg; …
WildDev
  • 2,250
  • 5
  • 35
  • 67