synchronization

List<T> doesn't implements SyncRoot!

佐手、 提交于 2019-11-29 18:16:36
问题 Everyone use lot of List. I need to iterate over this list, so I use the known SyncRoot pattern. Recently I noticed in this post that the SyncRoot should be avoided in favor of "embedded" thread-safety (each method will lock on an private object without exposing it using SyncRoot property). I can understand it, and partially I agree on that. The question is that List<T> class doesn't implements the SyncRoot property, even if implements the ICollection interface, which expose the SyncRoot

How to use shared memory in python and C/C++

血红的双手。 提交于 2019-11-29 15:51:03
问题 I am trying to modify a python program to be able to communicate with a C++ program using shared memory. The main responsibility of the python program is to read some video frames from an input queue located in shared memory, do something on the video frame and write it back to the output queue in shared memory. I believe there are few things I need to achieve and it would be great if someone can shed some light on it: Shared memory: In C/C++, you can use functions like shmget and shmat to

Efficient ways to append new data in Matlab (with example code)

可紊 提交于 2019-11-29 15:15:42
I am looking for methods, built-in functions, good practices... to append new data to a matrix - when the rows and columns are not the same The data I deal with is structured as follows: A.values: Ta x Ma matrix of values A.dates: Ta x 1 vector of datenum A.id: 1 x Ma cell array of ids Now the challenge is how to deal with new (potentially overlapping) data B that I load in and would like to append to a new matrix C : When new data comes in, it can expand both horizontally and vertically due to: new ids new dates It also can have dates that start before min(A.dates) or after max(A.dates) or

Java. Serialization of objects in a multithreaded environment

徘徊边缘 提交于 2019-11-29 15:11:07
I have an object whose internal mutable state is being constantly updated by one or more threads. The object is synchronized, and the goal is to periodically save its state (via serialization) from yet another thread: public class Counter implements Serializable { private int dogCount; private int catCount; public synchronized void updateFromDogThread( int count ) { dogCount = count; } public synchronized void updateFromCatThread( int count ) { catCount = count; } } Questions: Is serialization safe in this case? How does it work under the hood? That is to say, will the ObjectOutputStream

Synchronization on immutable objects (in java)

走远了吗. 提交于 2019-11-29 14:36:58
Code snippet - 1 class RequestObject implements Runnable { private static Integer nRequests = 0; @Override public void run() { synchronized (nRequests) { nRequests++; } } } Code snippet - 2 public class Racer implements Runnable { public static Boolean won = false; @Override public void run() { synchronized (won) { if (!won) won = true; } } } I was having a race condition with the first code snippet. I understood that this was because I was obtaining a lock on an immutable object(of type Integer). I have written a second code snippet which is again impervious to 'Boolean' being immutable. But

AtomicBoolean vs Synchronized block, whats the difference

旧城冷巷雨未停 提交于 2019-11-29 14:15:28
I am trying to understand the difference between the two following code blocks AtomicBoolean ab = new AtomicBoolean(false); using the following to get and set state. . ab.get(); ab.set(X); vs. private boolean ab = false; private final Object myboollock = new Ojbect(); public void setAB(boolean state) { synchronized(myboollock) { ab = state; } } public boolean getAB() { synchronized(myboollock) { return ab; } } I need to thread protect a boolean, that is all, and have in the past used the later method, but would like to start to use Atomic objects, (if ) they are safe?, There are a few subtle

Intel Inspector reports a data race in my spinlock implementation

强颜欢笑 提交于 2019-11-29 14:08:06
问题 I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable); The program seems to work OK (it gives the same result every time, which is not the case when no synchronization is used), but Intel Parallel Inspector says that there is a race condition at value += j (see the code below). The warning disappears when using Critical Sections instead of my SpinLock. Is my implementation of SpinLock correct or not ?

Is LocalStorage synchronization for Chrome extensions already available?

血红的双手。 提交于 2019-11-29 13:08:25
I found chromium issue titled "Allow extensions/apps to sync their own settings" that has status ' Fixed '. It's from December 2011. So, does it work? If so, how does it work and where are the docs? Last time similar question was asked in 2010 and then the answer was to use bookmarks hack. I think it's time to have an update on this topic. Update : I just found these docs for experimental chrome.storage API. Is this what I'm looking for? Do we have any new alternatives (other than bookmarks) till chrome.storage goes stable? chrome.experimental.storage was introduced as an experimental API in

How to set a variable that represents a time in the future in absolute terms Objective-C

大憨熊 提交于 2019-11-29 13:04:51
Motivation: I'm working on an app that makes several (client) phones read audio data from a (server) phone. The idea is that they must all play the song at the exact same time together. Premise: I must figure out a way to make all phones start at a certain time stamp that is absolute (ie it's not relative to the use set clock of either phone etc..).. based on some research I figured the best way to do this is to use CFAbsoluteTimeGetCurrent(); The idea here is that I get the latency it takes the server to communicate with each phone (b/c GKSession is done serially not in parallel apparently),

When to use synchronized in Java

随声附和 提交于 2019-11-29 13:03:45
问题 I hope this is going to be enough information, so here it goes. If you need more info, lemme know in the comments. I have a class that has two inner classes. The inner classes each have two methods that call a method in the outer class. So, it looks like this: public OuterClass { private boolean outerMethodHasBeenCalled = false; private void outerMethod() { if(!outerMethodHasBeenCalled) { // do stuff } outerMethodHasBeenCalled = true; } private FirstInnerClass { public void someMethod() {