double-checked-locking

Double checked locking with ConcurrentMap

北慕城南 提交于 2019-12-05 13:55:29
问题 I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap . I need to make this code thread safe and avoid unnecessary calls to initialize the shared resource. Here's the buggy code: private ConcurrentMap<String, Resource> map; // ..... String key = "somekey"; Resource resource; if (map.containsKey(key)) { resource = map.get(key); } else { resource = getResource(key);

Is this broken double checked locking?

前提是你 提交于 2019-12-05 09:56:17
Checkstyle reports this code as "The double-checked locking idiom is broken", but I don't think that my code actually is affected by the problems with double-checked locking. The code is supposed to create a row in a database if a row with that id doesn't exist. It runs in a multi-threaded environment and I want to avoid the primary-key-exists SQL-exceptions. The pseudo-code: private void createRow(int id) { Row row = dao().fetch(id); if (row == null) { synchronized (TestClass.class) { row = dao().fetch(id); if (row == null) { dao().create(id); } } } } I can agree that it looks like double

why using volatile with synchronized block?

浪尽此生 提交于 2019-12-05 08:24:36
问题 I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as volatile and they sychronized the block that initializes that instance ... My question is why we declare it volatile while we synch on it, why we need to do both?? isn't one of them is sufficient for the other ?? public class someClass { volatile static

Why Double-Checked Locking is used at all?

拥有回忆 提交于 2019-12-05 01:53:49
问题 I keep on running across code that uses double-checked locking, and I'm still confused as to why it's used at all. I initially didn't know that double-checked locking is broken, and when I learned it, it magnified this question for me: why do people use it in the first place? Isn't compare-and-swap better? if (field == null) Interlocked.CompareExchange(ref field, newValue, null); return field; (My question applies to both C# and Java, although the code above is for C#.) Does double-checked

Double checked locking with regular HashMap

大憨熊 提交于 2019-12-05 00:30:21
问题 Back to concurrency. By now it is clear that for the double checked locking to work the variable needs to be declared as volatile . But then what if double checked locking is used as below. class Test<A, B> { private final Map<A, B> map = new HashMap<>(); public B fetch(A key, Function<A, B> loader) { B value = map.get(key); if (value == null) { synchronized (this) { value = map.get(key); if (value == null) { value = loader.apply(key); map.put(key, value); } } } return value; } } Why does it

How should “Double-Checked Locking” be implemented in Delphi?

点点圈 提交于 2019-12-05 00:27:49
问题 In C#, the following code (from this page) can be used to lazily instantiate a singleton class in a thread safe way: class Foo { private volatile Helper helper = null; public Helper getHelper() { if (helper == null) { lock(this) { if (helper == null) helper = new Helper(); } } return helper; } } What would be the equivalent thread safe Delphi code? The article also mentions two problems with Double Checked Locking in Java: it is possible that the new object is constructed before the helper

Explain race condition in double checked locking

百般思念 提交于 2019-12-04 14:02:10
void undefined_behaviour_with_double_checked_locking() { if(!resource_ptr) #1 { std::lock_guard<std::mutex> lk(resource_mutex); #2 if(!resource_ptr) #3 { resource_ptr.reset(new some_resource); #4 } } resource_ptr->do_something(); #5 } if a thread sees the pointer written by another thread, it might not see the newly-created instance of some_resource, resulting in the call to do_something() operating on incorrect values. This is an example of the type of race condition defined as a data race by the C++ Standard, and thus specified as undefined behaviour. Question > I have seen the above

Should this C# code be refactored to use the Lazy<T> class instead?

喜欢而已 提交于 2019-12-04 03:59:03
I have the following code which could be called via multiple web-requests at the same second. As such, I don't want the second+ request hitting the database, but waiting until the first one does. Should I refactor this to use the Lazy<T> keyword class instead? If 10 calls to a Lazy<T> piece of code occur at the same time, do 9 of those calls wait for the first one to complete? public class ThemeService : IThemeService { private static readonly object SyncLock = new object(); private static IList<Theme> _themes; private readonly IRepository<Theme> _themeRepository; <snip snip snip> #region

Double checked locking pattern: Broken or not?

心已入冬 提交于 2019-12-04 03:02:48
Why is the pattern considered broken? It looks fine to me? Any ideas? public static Singleton getInst() { if (instace == null) createInst(); return instace; } private static synchronized createInst() { if (instace == null) { instace = new Singleton(); } } It looks okay at first glance, but this technique has many subtle problems and should usually be avoided. For example, consider the following sequence of events: Thread A notices that the value is not initialized, so it obtains the lock and begins to initialize the value. The code generated by the compiler is allowed to update the shared

Double checked locking with ConcurrentMap

谁都会走 提交于 2019-12-04 00:28:48
I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap . I need to make this code thread safe and avoid unnecessary calls to initialize the shared resource. Here's the buggy code: private ConcurrentMap<String, Resource> map; // ..... String key = "somekey"; Resource resource; if (map.containsKey(key)) { resource = map.get(key); } else { resource = getResource(key); // I/O-bound, expensive operation map.put(key, resource); } With the above code, multiple threads may