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
4 answers

What is the best way to iterate over list

I have worked pretty much on collection but I have few doubts. I am aware that we can iterate list with iterator. Another way is that we can go through as below: for(int i=0; i
Nimesh
  • 794
  • 3
  • 8
  • 18
5
votes
1 answer

PyQt5 + Python 3: passing lists, dicts as signal arguments across threads

I am using pyqtSignal to send a python list as an argument from worker thread to main thread. When does qt create a copy of the object being passed as argument. According to:…
5
votes
3 answers

How to access an array thread safely in Java?

Are operations on arrays in Java thread safe? If not how to make access to an array thread safe in Java for both reads and writes?
user3692521
  • 2,563
  • 5
  • 27
  • 33
5
votes
4 answers

Thread safety in java for static methods

I have the following code //EDIT :- updated code with @Riaz's answer ( this code should be thread -safe now ) public final class MyClass { private static MyClass2 _class2; private MyClass() { } public static synchronized …
user2912902
  • 327
  • 1
  • 7
  • 17
5
votes
1 answer

Multiple threads waiting on the same event handle in C++

Summary If multiple threads are concurrently waiting on the same event handle, as in: WaitForSingleObject(theHandle, INFINITE); and the event is initialized to be manual-reset, as in: // manual-reset and initial-state set to true theHandle =…
5
votes
1 answer

Rails - Concurrency issue with puma workers

I have a Puma server configured to use two workers, each with 16 threads. And having config.threadsafe! disabled to allow threading using puma. Now I have a code, which I doubt not using threadsafety even though I have used Mutex as a constant in…
Parth
  • 1,281
  • 8
  • 17
5
votes
4 answers

.NET Controls: Why aren't all calls thread-safe?

After exploding with excitement over learning about how to make thread-safe calls to Windows Form Controls, it got me thinking... Why aren't all calls to Windows Form Controls thread-safe? Can anyone explain why? I would think it would reduce a…
J. Polfer
  • 12,251
  • 10
  • 54
  • 83
5
votes
1 answer

VB.NET: Do I need to call Thread.MemoryBarrier() before each read if I always complete my writes with Thread.MemoryBarrier()?

VB.Net does not have an equivalent of C# volatile keyword so you have to manually implement volatile which is usually done by calling Thread.MemoryBarrier() before read and after write. So something like this is equivalent to declaring C# volatile…
5
votes
2 answers

Why are locks used here?

I am currently reading Joe Albahari's Threading in C# e-book, and sometimes in his example code, he uses locks in places where I don't see any thread safety issue. Here, for example, he locks around writing to and reading from the _status field,…
dgmulf
  • 475
  • 1
  • 3
  • 5
5
votes
6 answers

Java ArrayList.add() method thread safe for purely parallel adding?

Consider a for-loop over a function that takes an ArrayList reference and adds an object to that ArrayList. I would now like to execute each function call in parallel. Is the ArrayList.add() method thread safe if I don't care about the sequence the…
madison54
  • 743
  • 2
  • 8
  • 19
5
votes
3 answers

How to get equivalent of printf_l on Linux?

This function exists on OS X and allows you to pass custom local to the function. setlocale is not thread-safe, and passing locale as parameter is. If there is no equivalent, any way of locale-independent printf, or printf just for doubles (%g) will…
Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
5
votes
2 answers

Is publishing of magic statics thread safe?

Imagine I have this: const string& get_name() { static auto* ptr_name=new string("Ron"); return *ptr_name; } If multiple threads are calling get_name is that UB or not?
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
4 answers

C# Random Number Generator getting stuck in a cycle

I am using .NET to create an artificial life program and I am using C#'s pseudo random class defined in a Singleton. The idea is that if I use the same random number generator throughout the application, I could merely save the seed and then reload…
Jean Azzopardi
  • 2,289
  • 23
  • 36
5
votes
2 answers

Returning pointers in a thread-safe way

Assume I have a thread-safe collection of Things (call it a ThingList), and I want to add the following function. Thing * ThingList::findByName(string name) { return &item[name]; // or something similar.. } But by doing this, I've delegated the…
Roddy
  • 66,617
  • 42
  • 165
  • 277
5
votes
1 answer

How to share hash reference in multithread perl?

How do I share the hash reference $ref across main thread and worker threads ? #!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my $ref = {}; $ref->{0}->{abc} = 123; $ref->{1}->{abc} = 223; printf( "%d\n",…
Jean
  • 21,665
  • 24
  • 69
  • 119