synchronization

Shared-memory IPC synchronization (lock-free)

早过忘川 提交于 2019-11-26 04:45:30
问题 Consider the following scenario: Requirements: Intel x64 Server (multiple CPU-sockets => NUMA) Ubuntu 12, GCC 4.6 Two processes sharing large amounts of data over (named) shared-memory Classical producer-consumer scenario Memory is arranged in a circular buffer (with M elements) Program sequence (pseudo code): Process A (Producer): int bufferPos = 0; while( true ) { if( isBufferEmpty( bufferPos ) ) { writeData( bufferPos ); setBufferFull( bufferPos ); bufferPos = ( bufferPos + 1 ) % M; } }

Static versus non-static lock object in synchronized block

可紊 提交于 2019-11-26 04:39:35
问题 Trying to visualize and understand synchronization . What are the differences between using a static lock object (code A) and a non-static lock object (code B) for a synchronized block ? How does it differ in practical applications? What are the pitfalls one would have that the other wouldn\'t? What are the criteria to determine which one to use? Code A public class MyClass1 { private static final Object lock = new Object(); public MyClass1() { //unsync synchronized(lock) { //sync } //unsync

Java Singleton and Synchronization

此生再无相见时 提交于 2019-11-26 04:29:50
问题 Please clarify my queries regarding Singleton and Multithreading: What is the best way to implement Singleton in Java, in a multithreaded environment? What happens when multiple threads try to access getInstance() method at the same time? Can we make singleton\'s getInstance() synchronized ? Is synchronization really needed, when using Singleton classes? 回答1: Yes, it is necessary. There are several methods you can use to achieve thread safety with lazy initialization: Draconian

How to protect resources that may be used in a multi-threaded or async environment?

余生长醉 提交于 2019-11-26 04:26:39
问题 I am working on a C# API that is used by a variety of consumers. This API provides access to a shared resource (in my case hardware that does serial communication) , that will often have a few different actors trying to use it concurrently. The issue I have is that some of my consumers will want to use this in a multi-threaded environment - each actor works independently and try to use the resource. A simple lock works fine here. But some of my consumers would prefer to use async-await and

When should one use a spinlock instead of mutex?

我与影子孤独终老i 提交于 2019-11-26 03:45:56
问题 I think both are doing the same job,how do you decide which one to use for synchronization? 回答1: The Theory In theory, when a thread tries to lock a mutex and it does not succeed, because the mutex is already locked, it will go to sleep, immediately allowing another thread to run. It will continue to sleep until being woken up, which will be the case once the mutex is being unlocked by whatever thread was holding the lock before. When a thread tries to lock a spinlock and it does not succeed,

Synchronizing on an Integer value [duplicate]

送分小仙女□ 提交于 2019-11-26 03:39:54
问题 Possible Duplicate: What is the best way to increase number of locks in java Suppose I want to lock based on an integer id value. In this case, there\'s a function that pulls a value from a cache and does a fairly expensive retrieve/store into the cache if the value isn\'t there. The existing code isn\'t synchronized and could potentially trigger multiple retrieve/store operations: //psuedocode public Page getPage (Integer id){ Page p = cache.get(id); if (p==null) { p=getFromDataBase(id);

Synchronizing on String objects in Java

走远了吗. 提交于 2019-11-26 03:18:55
问题 I have a webapp that I am in the middle of doing some load/performance testing on, particularily on a feature where we expect a few hundred users to be accessing the same page and hitting refresh about every 10 seconds on this page. One area of improvement that we found we could make with this function was to cache the responses from the web service for some period of time, since the data is not changing. After implementing this basic caching, in some further testing I found out that I didn\

C# version of java's synchronized keyword?

南楼画角 提交于 2019-11-26 02:39:36
问题 Does c# have its own version of the java \"synchronized\" keyword? I.e. in java it can be specified either to a function, an object or a block of code, like so: public synchronized void doImportantStuff() { // dangerous code goes here. } or public void doImportantStuff() { // trivial stuff synchronized(someLock) { // dangerous code goes here. } } 回答1: First - most classes will never need to be thread-safe. Use YAGNI: only apply thread-safety when you know you actually are going to use it (and

Java double checked locking

隐身守侯 提交于 2019-11-26 02:35:55
问题 I happened upon an article recently discussing the double checked locking pattern in Java and its pitfalls and now I\'m wondering if a variant of that pattern that I\'ve been using for years now is subject to any issues. I\'ve looked at many posts and articles on the subject and understand the potential issues with getting a reference to a partially constructed object, and as far as I can tell, I don\'t think my implementation is subject to these issues. Are there any issues with the

Are Java static initializers thread safe?

£可爱£侵袭症+ 提交于 2019-11-26 02:28:38
问题 I\'m using a static code block to initialize some controllers in a registry I have. My question is therefore, can I guarantee that this static code block will only absolutely be called once when the class is first loaded? I understand I cannot guarantee when this code block will be called, I\'m guessing its when the Classloader first loads it. I realize I could synchronize on the class in the static code block, but my guess is this is actually what happens anyway? Simple code example would be