synchronization

How to Lock a file and avoid readings while it's writing

两盒软妹~` 提交于 2019-12-04 03:26:32
My web application returns a file from the filesystem. These files are dynamic, so I have no way to know the names o how many of them will there be. When this file doesn't exist, the application creates it from the database. I want to avoid that two different threads recreate the same file at the same time, or that a thread try to return the file while other thread is creating it. Also, I don't want to get a lock over a element that is common for all the files. Therefore I should lock the file just when I'm creating it. So I want to lock a file till its recreation is complete, if other thread

Data Synchronization framework / algorithm for server<->device?

耗尽温柔 提交于 2019-12-04 03:25:32
I'm looking to implement data synchronization between servers and distributed clients. The data source on the server is mysql with django on top. The client can vary. Updates can take place on either client or server, and the connection between server and client is not reliable (eg. changes can be made on a disconnected cell phone, should get sync'd when the cell phone has a connection again). S. Lott suggests using a version control design pattern in this question , which makes sense. I'm wondering if there are any existing packages / implementations of this I can use. Or, should I directly

How to properly leave a Critical Section?

牧云@^-^@ 提交于 2019-12-04 03:22:06
I have the following C++ code where I make use of the Critical Section object : EnterCriticalSection(&cs); // code that may throw an exception LeaveCriticalSection(&cs); How can I ensure that the LeaveCriticalSection function is called even if an exception is thrown? Just write a guard utilizing the destructor for clean up: struct Guard { CriticalSection& cs; Guard(CriticalSection& cs) : cs(cs) { EnterCriticalSection(cs); } ~Guard() { LeaveCriticalSection(cs); } Guard(const Guard&) = delete; Guard& operator = (const Guard&) = delete; }; Usage: void f() { Guard guard(cs); ... } marcinj Use RAII

How do I make a critical section with Boost?

余生颓废 提交于 2019-12-04 02:57:56
问题 For my cross-platform application I have started to use Boost, but I can't understand how I can implement code to reproduce behavior of Win32's critical section or .Net's lock . I want to write a method Foo that can be called from different threads to control write operations to shared fields. Recursive calls within the same thread should be allowed (Foo() -> Foo()). In C# this implementation is very simple: object _synch = new object(); void Foo() { lock (_synch) // one thread can't be lock

Implementation of the addAndGet in AtomicInteger class

Deadly 提交于 2019-12-04 01:32:47
问题 I was going through the Java(Java 6) souce code for the addAndGet method in the AtomicInteger class. The corresponding code was as follows: public final int addAndGet(int delta) { for (;;) { int current = get(); int next = current + delta; if (compareAndSet(current, next)) return next; } } The compareAndSet method calls a native method to carry out the assignment. There are mainly two questions: How does the infinite loop help ? What could be the scenarios, under which the "if (compareAndSet

Cost of synchronization

醉酒当歌 提交于 2019-12-04 00:42:14
问题 In a highly concurrent Java program and assuming that my methods are correctly written and correctly synchronized, I am wondering about how to determine which is better: void synchronized something() { ... } or void something() { synchronized(this) { ... } // here do stuff no requiring synchronization . . // do computation 'A' . synchronized(this) { ... } // here do other stuff no requiring synchronization . . // do computation 'B' . synchronized(this) { ... } } Now I realize that if

Double checked locking with ConcurrentMap

谁都会走 提交于 2019-12-04 00:28:48
I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap . I need to make this code thread safe and avoid unnecessary calls to initialize the shared resource. Here's the buggy code: private ConcurrentMap<String, Resource> map; // ..... String key = "somekey"; Resource resource; if (map.containsKey(key)) { resource = map.get(key); } else { resource = getResource(key); // I/O-bound, expensive operation map.put(key, resource); } With the above code, multiple threads may

How can I get the table name in a PostgreSQL trigger function?

守給你的承諾、 提交于 2019-12-03 23:29:56
I have a trigger function: CREATE OR REPLACE FUNCTION "trigger_deleteUsers"() RETURNS trigger AS $BODY$ BEGIN INSERT INTO "DeletedEntities" ("uuidKey", "dateCreated", "dateModified", "dateSynced", "username", "entityName") VALUES (OLD."uuidKey", OLD."dateCreated", OLD."dateModified", "dateSynced", OLD."username", 'Users'); RETURN NULL; END; $BODY$ LANGUAGE plpgsql; CREATE TRIGGER "deleteUsers" AFTER DELETE ON "Users" FOR EACH ROW EXECUTE PROCEDURE "trigger_deleteUsers"(); This works for the table "Users". Every time I delete a row from the "Users" table the database inserts a row with (

Force order of execution of C statements?

与世无争的帅哥 提交于 2019-12-03 23:08:29
I have a problem with the MS C compiler reordering certain statements, critical in a multithreading context, at high levels of optimization. I want to know how to force ordering in specific places while still using high levels of optimization. (At low levels of optimization, this compiler does not reorder statements) The following code: ChunkT* plog2sizeChunk=... SET_BUSY(plog2sizeChunk->pPoolAndBusyFlag); // set "busy" bit on this chunk of storage x = plog2sizeChunk->pNext; produces this: 0040130F 8B 5A 08 mov ebx,dword ptr [edx+8] 00401312 83 22 FE and dword ptr [edx],0FFFFFFFEh in which the

What's the point of this synchronization?

佐手、 提交于 2019-12-03 22:33:02
问题 What is the point of the synchronization here? Why not just use mConnectedThread.write(out) ? The code snippet is from the BluetoothChat sample for Android (found here) /** * Write to the ConnectedThread in an unsynchronized manner * @param out The bytes to write * @see ConnectedThread#write(byte[]) */ public void write(byte[] out) { // Create temporary object ConnectedThread r; // Synchronize a copy of the ConnectedThread synchronized (this) { if (mState != STATE_CONNECTED) return; r =