Questions tagged [atomicreference]

Java atomic.AtomicReference. (Use [stdatomic] for questions about C++20 std::atomic_ref)

Java

java.util.concurrent.atomic.AtomicReference<V> was new in Java 1.5

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicReference.html

C++ - use for C++ atomic_ref<T>

std::atomic_ref<T> was new in C++20, allowing atomic ops on properly-aligned plain objects. Such as alignas(std::atomic_ref<long>::required_alignment) long foo;

https://en.cppreference.com/w/cpp/atomic/atomic_ref

55 questions
3
votes
1 answer

java - sharing data between threads - atomicreference or synchronize

I am making a 2 player videogame, and the oponent's position gets updated on a thread, because it has a socket that is continuously listening. What I want to share is position and rotation. As it is a videogame I don't want the main thread to be…
dyeray
  • 1,056
  • 6
  • 17
2
votes
0 answers

Reading data from a Database in a thread-safe manner

In my app I have a "Manager" class that has a reference to a DAO class which loads data from DB and populate a HashMap as a cache solution. Here is a snippet class UserManager { private final UserDAO userDAO; private final Map
Shvalb
  • 1,835
  • 2
  • 30
  • 60
2
votes
1 answer

Correct usage of volatile with std::atomic_ref

I'm having trouble wrapping my head around the correct usage of std::atomic_ref with volatile. Naively there are three possibilities: std::atomic_ref ref1; volatile std::atomic_ref ref2; volatile std::atomic_ref
iko
  • 53
  • 1
  • 5
2
votes
2 answers

AtomicReference not working to avoid race condition in java multi threading

I have a "User.java" class that has Integer variable count initially set to 0. In another class "ThreadDemo.java" I have set the User object in AtomicReference. This "userRef" object is shared by "1000" threads and in each thread I am incrementing…
2
votes
1 answer

Should you use the AtomicReference for the Singleton Pattern?

I ran across the AtomicReference class and was wondering if this could be a good way of creating a Singleton that is mutable and could be replaced for testing. I know the double-lock checking has issues so I didn't want to go that route. Corrected…
Alex Beggs
  • 1,187
  • 2
  • 11
  • 17
2
votes
2 answers

Data Races in an AtomicIntegerArray

In the code below: I am updating num[1]=0 of an AtomicIntegerArray num 1000 times each in 2 threads. At the end of the 2 threads in main thread ;shouldn't the value of num[1] be 2000 as there shouldn't be data races in an AtomicIntegerArray…
2
votes
1 answer

Using lock-free algorithm to put values into a Map of custom objects

I have a Map. In order to update the key, I need to check if it already exists. Else, I need to create a new Object and put it. Map map = new ConcurrentHashMap(); My function is this put(Object value) { if(!map.containsKey(K…
user592748
  • 1,194
  • 3
  • 21
  • 45
2
votes
2 answers

Limiting concurrent access to a field

So in a program I'm writing I use a bi-directional breadth first search to search a graph. I do this by running 1 breadth first search in 1 thread, and one in another. Now the search is said to have found the optimal solution when it either an…
Ethan
  • 1,206
  • 3
  • 21
  • 39
1
vote
1 answer

Java 8 impl on Compare And Exchange (Not Compare and Set!)

In java 17, AtomicReference has the compareAndExchange method which is like compareAndSet, but instead of returning a boolean, it returns the value right before the atomic action. I need that to implement a custom concurrent structure. Due to…
1
vote
0 answers

Updating parameterized type AtomicReference with UnaryOperator, does it loses its thread safety properties?

Lets say I have the next method: public synchronized void remap(UnaryOperator update) { this.memory = update.apply(this.memory); inferDispatch(); } Where memory is a volatile X variable; In order to update X I could either setX(X x),…
Delark
  • 1,141
  • 2
  • 9
  • 15
1
vote
0 answers

AtomicReference#compareAndSet bogs down main thread, should I use synchronized?

I've built a collection of atomic maps, sets and lists classes using AtomicReference. I've ran into a problem where when AtomicReference#compareAndSet is called ~20 times a second there is severe lag. (AtomicSet doesn't cause any lag, but that's…
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
1
vote
0 answers

Appropriate atomic pointer to use in this case

Let's say I have an implementation of the Herlihy-Wing Queue in Java : public class HWQueue { AtomicReference[] items; AtomicInteger tail; static final int CAPACITY = 1024; public HWQueue() { items…
Jarvis
  • 8,494
  • 3
  • 27
  • 58
1
vote
1 answer

Singleton with arguments using AtomicReference

I have to create a Singleton which takes input arguments . Basically I need to create a DBConnector in a library based on some configuration . Now this configuration is passed to the library by the consuming app . Based on the passed in…
A.K.
  • 13
  • 3
1
vote
1 answer

AtomicReferences for Intents and Activity Navigation in Helper File

In my app, I navigate between about 5 different screens, each in its own activity. Pretty much any activity can be called from any other activity, so I am trying to build a helper file to manage the intents so that I don’t have redundant code. I…
1
vote
2 answers

Data Races on individual elements of an AtomicReference

I had a question related to accessing individual elements via an Atomic Reference. If I have an IntegerArray and an atomic reference to it;will reading and writing to individual elements of the array via the AtomicReference variable cause data…