atomicity

Simulate tearing a double in C#

对着背影说爱祢 提交于 2019-11-27 15:50:39
问题 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) {

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

三世轮回 提交于 2019-11-27 15:17:31
问题 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

should LOCK_EX on both read & write be atomic?

自作多情 提交于 2019-11-27 12:45:31
file_put_contents ( "file", "data", LOCK_EX ) for writing (which means - aquire lock and write) file_get_contents ( "file", LOCK_EX ) for reading (which means - aquire lock and then read) will it throw exception? raise an error? block until lock is aquired? or at least - should it ? is there a chance that php will behave like this one day? EDIT: i know that it is possible to use rename - i'd like to know answer to this... ircmaxell Since this answer is long, here's the summary: No, file_get_contents() is not atomic as it does not respect advisory locks . About file locks in PHP: In PHP, while

Avoid duplicate POSTs with REST

你。 提交于 2019-11-27 11:07:22
I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the 201 Created response. The client only sees a failed POST request, and tries again later, and the server happily creates a duplicate object... Others must have had this problem, right? But I google around, and everyone just seems to ignore it. I have 2 solutions: A) Use PUT instead, and create the (GU)ID on the client. B) Add a GUID to all objects created on the client, and have the server enforce their UNIQUE -ness. A doesn

Atomic Operations and multithreading

China☆狼群 提交于 2019-11-27 10:49:53
问题 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

When do I really need to use atomic<bool> instead of bool? [duplicate]

江枫思渺然 提交于 2019-11-27 06:39:23
This question already has an answer here: Can a bool read/write operation be not atomic on x86? [duplicate] 3 answers Do I have to use atomic<bool> for “exit” bool variable? 3 answers Isn't atomic<bool> redundant because bool is atomic by nature? I don't think it's possible to have a partially modified bool value. When do I really need to use atomic<bool> instead of bool ? No type in C++ is "atomic by nature" unless it is an std::atomic* -something. That's because the standard says so. In practice, the actual hardware instructions that are emitted to manipulate an std::atomic<bool> may (or may

Is rename() atomic?

与世无争的帅哥 提交于 2019-11-27 04:28:10
I am not being able to check this via experiments and could not gather it from the man pages as well. Say I have two processes, one moving(rename) file1 from directory1 to directory2. Say the other process running concurrently copies the contents of directory1 and directory2 to another location. Is it possible that the copy happens in such a way that both directory1 and directory2 will show file1 - i.e directory1 is copied before the move and directory2 after the move by the first process. Basically is rename() is an atomic system call? Thanks Yes and no. rename() is atomic assuming the OS

Django - Rollback save with transaction atomic

人走茶凉 提交于 2019-11-27 01:16:21
问题 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

Atomic file copy under .NET

萝らか妹 提交于 2019-11-26 21:48:35
问题 I am building a server app that copies files using System.IO.File.Copy(...) function. My files can be rather large, therefore, it has a fair chance that if the machine crashes, it happens during copying. After restarting the service, I should be able to pick up the copy tasks and continue. How can I detect if a copy has been successfully completed or interrupted by server crash? My current plan is to copy the files to a temporary name and once copying completed rename it to the final name.

Avoid duplicate POSTs with REST

血红的双手。 提交于 2019-11-26 15:19:35
问题 I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the 201 Created response. The client only sees a failed POST request, and tries again later, and the server happily creates a duplicate object... Others must have had this problem, right? But I google around, and everyone just seems to ignore it. I have 2 solutions: A) Use PUT instead, and create the (GU)ID on the client. B)