atomicity

Atomic Instruction

白昼怎懂夜的黑 提交于 2019-11-29 11:07:50
What do you mean by Atomic instructions? How does the following become Atomic? TestAndSet int TestAndSet(int *x){ register int temp = *x; *x = 1; return temp; } From a software perspective, if one does not want to use non-blocking synchronization primitives, how can one ensure Atomicity of instruction? is it possible only at Hardware or some assembly level directive optimization can be used? Some machine instructions are intrinsically atomic - for example, reading and writing properly aligned values of the native processor word size is atomic on many architectures . This means that hardware

Is pointer assignment atomic in C++?

好久不见. 提交于 2019-11-29 09:25:41
I've actually heard claims both ways. I suspect they are not, but I wanted to get the topic settled. C++03 does not know about the existance of threads, therefore the concept of atomicity doesn't make much sense for C++03, meaning that it doesn't say anything about that. C++11 does know about threads, but once again doesn't say anything about the atomicity of assigning pointers. However C++11 does contain std::atomic<T*> , which is guaranteed to be atomic. Note that even if writing to a raw pointer is atomic on your platform the compiler is still free to move that assingment around, so that

Simulate tearing a double in C#

别来无恙 提交于 2019-11-29 01:21:00
I'm running on a 32-bit machine and I'm able to confirm that long values can tear using the following code snippet which hits very quickly. static void TestTearingLong() { System.Threading.Thread A = new System.Threading.Thread(ThreadA); A.Start(); System.Threading.Thread B = new System.Threading.Thread(ThreadB); B.Start(); } static ulong s_x; static void ThreadA() { int i = 0; while (true) { s_x = (i & 1) == 0 ? 0x0L : 0xaaaabbbbccccddddL; i++; } } static void ThreadB() { while (true) { ulong x = s_x; Debug.Assert(x == 0x0L || x == 0xaaaabbbbccccddddL); } } But when I try something similar

what is “failure atomicity” used by J bloch and how its beneficial in terms of immutable object?

天大地大妈咪最大 提交于 2019-11-29 00:36:12
问题 just came across below statement as benefit of immutable object Immutable object always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state. can any one explain it in more detail and why is it so? 回答1: Bloch's "Failure atomicity" means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before

How do I prevent duplicate voting while incrementing the vote count on Firebase?

左心房为你撑大大i 提交于 2019-11-29 00:16:52
I'm building a real-time poll in Firebase. Each vote is stored in a list field. To prevent having to pull every vote down to the client in order to count them, I cache the tallies for each choice in counter fields. poll1 counts choice1: 5 choice2: 2 choice3: 10 choice4: 252 voters uid1 : choice1 uid6 : choice3 uid25: choice2 uid31: choice1 I'm currently updating the counter with the following transaction: var fireCountPush = new Firebase(self.fireCountUrl+node+id); fireCountPush.transaction(function(current_value) { return current_value + 1; }, function(error, committed, snapshot) { if

Atomic Operations and multithreading

寵の児 提交于 2019-11-28 17:52:11
Recently I was reading a tutorial, in that I came across a statement that says.. "The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long or double ). Operations variables of type long or double are only atomic if they declared with the volatile keyword." AtomicInteger or AtomicLong that provides methods like getAndDecrement() , getAndIncrement() and getAndSet() which are atomic. I got confused a bit with the above statement.. could you please clarify when to use AtomicInteger or AtomicLong classes. JB Nizet Doing

AtomicBoolean vs Synchronized block, whats the difference

南笙酒味 提交于 2019-11-28 08:13:07
问题 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

Atomic properties vs thread-safe in Objective-C

点点圈 提交于 2019-11-28 04:42:42
In most of the discussions I've read, it indicates that making a property atomic does not guarantee it to be thread-safe, it just guarantees that the value returned won't be garbage as a result of one object writing to it and another trying to read it at the same time. I understand this isn't thread-safe as a third object could be writing it and while the object accessing it wouldn't get garbage back, it's not entirely certain which value it will get back as multiple objects are writing to it at the same time, and it may get any of their values. So when we say it won't return garbage, would

On a multicore x86, is a LOCK necessary as a prefix to XCHG?

戏子无情 提交于 2019-11-27 21:15:13
问题 If mem is a shared memory location, do I need: XCHG EAX,mem or: LOCK XCHG EAX,mem to do the exchange atomically? Googling this yields both yes and no answers. Does anyone know this definitively? 回答1: Intel's documentation seems pretty clear that it is redundant. IA-32 Intel® Architecture Software Developer’s Manual Volume 3A: System Programming Guide, Part 1 7.1.2.1 says: The operations on which the processor automatically follows the LOCK semantics are as follows: When executing an XCHG

Django - Rollback save with transaction atomic

十年热恋 提交于 2019-11-27 20:06:25
I am trying to create a view where I save an object but I'd like to undo that save if some exception is raised. This is what I tried: class MyView(View): @transation.atomic def post(self, request, *args, **kwargs): try: some_object = SomeModel(...) some_object.save() if something: raise exception.NotAcceptable() # When the workflow comes into this condition, I think the previous save should be undome # Whant am I missing? except exception.NotAcceptable, e: # do something What am I doing wrong? even when the exception is raised some_object is still in DataBase. Atomicity Documentation To