atomicity

compare and swap vs test and set

余生颓废 提交于 2019-12-03 03:13:52
问题 Could someone explain to me the working and differences of above operations in multi-threading? 回答1: test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same , modifies the contents of that memory location to a given new value. The difference marked in bold. 回答2: Test and set operates on a bit, compare and swap operates

Is the “switch” statement evaluation thread-safe?

泄露秘密 提交于 2019-12-03 01:16:56
Consider the following sample code: class MyClass { public long x; public void DoWork() { switch (x) { case 0xFF00000000L: // do whatever... break; case 0xFFL: // do whatever... break; default: //notify that something going wrong throw new Exception(); } } } Forget the uselessness of the snippet: my doubt is about the behavior of the switch statement. Suppose that the x field could have only two values: 0xFF00000000L or 0xFFL . The switch above should not fall into the "default" option. Now imagine that one thread is executing the switch with "x" equal to 0xFFL, thus the first condition won't

How are atomic operations implemented at a hardware level?

≯℡__Kan透↙ 提交于 2019-12-03 01:02:59
问题 I get that at the assembly language level instruction set architectures provide compare and swap and similar operations. However, I don't understand how the chip is able to provide these guarantees. As I imagine it, the execution of the instruction must Fetch a value from memory Compare the value Depending on the comparison, possibly store another value in memory What prevents another core from accessing the memory address after the first has fetched it but before it sets the new value? Does

compare and swap vs test and set

不问归期 提交于 2019-12-02 16:44:19
Could someone explain to me the working and differences of above operations in multi-threading? test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same , modifies the contents of that memory location to a given new value. The difference marked in bold. Test and set operates on a bit, compare and swap operates on a 32-bit field. The z/TPF system favors the use of the test and set (TS) instruction because frequently, lock

Counting the number of exceptions happening in catch block

亡梦爱人 提交于 2019-12-02 12:40:10
问题 I am trying to collect all the counts of exception happening and the name of exception in a ConcurrentHashMap so that I should be aware of how many times this exception has occurred. So in my catch block I have map that will keep on adding the name of exception and there total count occurrence. Below is my code which I have modified to always throw SQL Exception everytime for the testing purpose so that I can see the count of exception is accurate or not. So certain scenario- 1) If I am

Counting the number of exceptions happening in catch block

一笑奈何 提交于 2019-12-02 03:55:24
I am trying to collect all the counts of exception happening and the name of exception in a ConcurrentHashMap so that I should be aware of how many times this exception has occurred. So in my catch block I have map that will keep on adding the name of exception and there total count occurrence. Below is my code which I have modified to always throw SQL Exception everytime for the testing purpose so that I can see the count of exception is accurate or not. So certain scenario- 1) If I am choosing number of threads as 10 and number of tasks as 50 , then in that map, I can see 500 exception for

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

独自空忆成欢 提交于 2019-12-02 01:16:11
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.asynchronous(new CustomRemovalListener(), executorService)) .build(); private static class Holder { private

Some doubts about volatile and Atomic classes?

隐身守侯 提交于 2019-12-01 23:49:03
问题 i am going thru Java threads book. I came across this statement Statement 1:- "volatile variables can be safely used only for single load or store operation and can't be applied to long or double variales. These restrictions make the use of volatile variables uncommon" I did not get what does single load or store operation mean here? why volatile can't be applied to long or double variales? Statement 2:- "A Volatile integer can not be used with the ++ operator because ++ operator contains

Some doubts about volatile and Atomic classes?

安稳与你 提交于 2019-12-01 21:40:50
i am going thru Java threads book. I came across this statement Statement 1:- "volatile variables can be safely used only for single load or store operation and can't be applied to long or double variales. These restrictions make the use of volatile variables uncommon" I did not get what does single load or store operation mean here? why volatile can't be applied to long or double variales? Statement 2:- "A Volatile integer can not be used with the ++ operator because ++ operator contains multiple instructions.The AtomicInteger class has a method that allows the integer it holds to be

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

徘徊边缘 提交于 2019-12-01 19:38:16
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 writers What happens if a write system call is called on same file by 2 different processes