atomicity

How to empty Guava cache every 30 seconds while sending it to another method?

纵饮孤独 提交于 2019-12-31 03:26:13
问题 I am populating my guava cache from multiple threads by calling add method. Now from the background thread which runs every 30 seconds, I want to send whatever is there in the cache to sendToDB method atomically? Below is my code: public class Example { private final ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor(); private final Cache<Integer, List<Process>> cache = CacheBuilder.newBuilder().maximumSize(100000) .removalListener(RemovalListeners

How does one programmatically determine if “write” system call is atomic on a particular file?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 00:45:18
问题 In some cases the coder cannot rely on system calls being atomic, e.g. if the file is on a NFS filesystem. (c.f. NFS Overview, FAQ and HOWTO Documents). But atomic system calls are ultimately required for most database work. (c.f. Atomicity of database systems). Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python). Any suggestions? Subsequent notes: Atomicity on pipes is discussed in the following: unix pipe

Multiple threads arrive, the last should do the processing

浪子不回头ぞ 提交于 2019-12-25 18:50:41
问题 I'm implementing a logging where multiple threads can write into one List of log. The last thread should write the contents of the List to a file. So the thread which is the last to write into the List should flush the List into a file. What is the best way to accomplish this? For the List I just need one of the concurrent classes that is efficient for multiple writers and one reader. 回答1: In my case the simple solution was to implement Closeable and then do the flushing in the close method.

.net System.MemberwiseClone and interlocked writes

落爺英雄遲暮 提交于 2019-12-25 08:15:37
问题 When performing a MemberwiseClone of an array of value types: var arr = new double[100]; If these doubles are being modified using an Interlocked write on other threads, will the MemberwiseCloned copy be at any risk of having torn doubles in it? I'm not concerned about having slightly stale values, just tearing and the interaction between interlocked and memberwiseclone (which I guess translates to a memory blit type operation?) 回答1: Yes. On 32bit operating systems this is even guaranteed to

Synchronization across threads / atomic checks?

牧云@^-^@ 提交于 2019-12-24 16:24:26
问题 I need to create an method invoker that any thread (Thread B for example sake) can call, which will execute on the main executing thread (Thead A) at a specific given point in its execution. Example usage would be as follows: static Invoker Invoker = new Invoker(); static void ThreadA() { new Thread(ThreadB).Start(); Thread.Sleep(...); // Hypothetic Alpha Invoker.Invoke(delegate { Console.WriteLine("Action"); }, true); Console.WriteLine("Done"); Console.ReadLine(); } static void ThreadB() {

How to make concurrent hash map thread safe with get and put as an atomic operation?

ⅰ亾dé卋堺 提交于 2019-12-24 07:56:50
问题 Is my below method thread safe? This method is in Singleton class. private static final Map<String, PreparedStatement> holder = new ConcurrentHashMap<>(); public BoundStatement getStatement(String cql) { Session session = TestUtils.getInstance().getSession(); PreparedStatement ps = holder.get(cql); if(ps == null) { // If "ps" is already present in cache, then we don't have to synchronize and make threads wait. synchronized { ps = holder.get(cql); if (ps == null) { ps = session.prepare(cql);

Atomicity of Reads and Writes for Reference Variables in Java

我们两清 提交于 2019-12-24 05:58:55
问题 First the quote from From JLS 8 Sec 17.7 Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values. Here is the scenario that confuses me, given Employee class and a method within this class called calculate which returns a reference to an instance of Employee. Employee emp = calculate(); When a write to variable is atomic, it means that no other thread can access that variable until the atomic operation is done, and in the

Atomicity of findAndModify on embedded documents

你离开我真会死。 提交于 2019-12-24 01:44:49
问题 On mongodb manual there is an example for atomic operations on a single document. book = { _id: 123456789, title: "MongoDB: The Definitive Guide", available: 3, checkout: [ { by: "joe", date: ISODate("2012-10-15") } ] } The manual states that the below operation is atomic: db.books.findAndModify ( { query: { _id: 123456789, available: { $gt: 0 } }, update: { $inc: { available: -1 }, $push: { checkout: { by: "abc", date: new Date() } } } } ) My question is what would happen if available field

Can variables inside packed structures be read atomically?

半城伤御伤魂 提交于 2019-12-23 19:28:28
问题 I'm writing code for a Cortex M0 (ARM) CPU, and 32-bit reads/writes are atomic. Now I was wondering when I read/write 8bit/16bit variables, are they also guaranteed to be atomic? My instinct says yes, because they are internally aligned to 32-bit sections, so there is no possibility that the CPU needs two separate instructions to read/write them. But I also happen to store a lot of variables in packed structures to save memory, and there it's possible that variables are not aligned on 32-bit

Are 64 bit operations atomic for a 32 bit app on 64 bit Windows

こ雲淡風輕ζ 提交于 2019-12-23 03:14:51
问题 So this document says that running 64 bit Windows gives you 64 bit atomicity: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684122%28v=vs.85%29.aspx This post indicates that you have to run a 64 bit app to gain 64 bit atomicity: atomicity in 32/64 bit I'm developing a Win32 console app. So, if I understand correctly, I have to use 32 bit types to get atomicity, right? I cannot assume a 64 bit type has atomic writes/reads? 回答1: In a 64-bit app, 64-bit read/write operations can be