synchronization

How to acquire a lock by a key

戏子无情 提交于 2019-11-26 22:39:14
What is the best way to prevent concurrent update of one record in a key-value set without locking the entire set? Semantically, I'm looking for some kind of locking by a key (ideally, Java implementation, but not necessarily): interface LockByKey { void lock(String key); // acquire an exclusive lock for a key void unlock(String key); // release lock for a key } This lock is intended to synchronize an access to a remote store, so some synchronized Java collection is not an option. Louis Wasserman Guava has something like this being released in 13.0; you can get it out of HEAD if you like.

WaitForSingleObject and WaitForMultipleObjects equivalent in Linux?

谁都会走 提交于 2019-11-26 22:15:24
I am migrating an applciation from windows to linux. I am facing problem with respect to WaitForSingleObject and WaitForMultipleObjects interfaces. In my application I spawn multiple threads where all threads wait for events from parent process or periodically run for every t seconds. I have checked pthread_cond_timedwait , but we have to specify absolute time for this. How can I implement this in Unix? Stick to pthread_cond_timedwait and use clock_gettime . For example: struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 10; // ten seconds while (!some_condition && ret == 0)

Make previous memory stores visible to subsequent memory loads

北慕城南 提交于 2019-11-26 22:10:45
问题 I want to store data in a large array with _mm256_stream_si256() called in a loop. As I understood, a memory fence is then needed to make these changes visible to other threads. The description of _mm_sfence() says Perform a serializing operation on all store-to-memory instructions that were issued prior to this instruction. Guarantees that every store instruction that precedes, in program order, is globally visible before any store instruction which follows the fence in program order. But

Dependent loads reordering in CPU

被刻印的时光 ゝ 提交于 2019-11-26 21:51:35
问题 I have been reading Memory Barriers: A Hardware View For Software Hackers, a very popular article by Paul E. McKenney. One of the things the paper highlights is that, very weakly ordered processors like Alpha, can reorder dependent loads which seems to be a side effect of partitioned cache Snippet from the paper: 1 struct el *insert(long key, long data) 2 { 3 struct el *p; 4 p = kmalloc(sizeof(*p), GPF_ATOMIC); 5 spin_lock(&mutex); 6 p->next = head.next; 7 p->key = key; 8 p->data = data; 9

Android application with phone book synchronization?

随声附和 提交于 2019-11-26 21:45:35
问题 I am creating one android test application , in which i have one button. On button click ,i want to synchronize phonebook records with my local database.If record in phone book is not in db table then insert it , otherwise leave it as it is. so how can i do this? 回答1: For getting contact list from your phone book you need write permission in AndroidManifest.XML (i.e. android.permission.READ_CONTACTS ) .And you can collect contact list using following method. ShowContact() { ArrayList<String>

Proper way for signal edge detection in Verilog

拥有回忆 提交于 2019-11-26 21:40:14
问题 I want to detect a rising edge of a signal from a flip-flop AA to BB +----+ A ----------------| |----- OUT +----+ | BB | B ----| |------|> | | AA | +----+ clk ----|> | +----+ Verilog code: module edge_detect ( input A, input B, input clk, output OUT ); reg AA; reg BB; always @(posedge clk) begin AA <= B; end always @(posedge AA)begin BB <= A; end assign OUT = BB; endmodule The output of AA is used as a clock to BB saying that AA has done its job and then BB can now continue its operation. I

Java synchronized block vs. Collections.synchronizedMap

本秂侑毒 提交于 2019-11-26 21:37:35
Is the following code set up to correctly synchronize the calls on synchronizedMap ? public class MyClass { private static Map<String, List<String>> synchronizedMap = Collections.synchronizedMap(new HashMap<String, List<String>>()); public void doWork(String key) { List<String> values = null; while ((values = synchronizedMap.remove(key)) != null) { //do something with values } } public static void addToMap(String key, String value) { synchronized (synchronizedMap) { if (synchronizedMap.containsKey(key)) { synchronizedMap.get(key).add(value); } else { List<String> valuesList = new ArrayList

What are the differences between various threading synchronization options in C#?

夙愿已清 提交于 2019-11-26 21:13:45
Can someone explain the difference between: lock (someobject) {} Using Mutex Using Semaphore Using Monitor Using Other .Net synchronization classes I just can't figure it out. It seems to me the first two are the same? Great question. I maybe wrong.. Let me try.. Revision#2 of my orig answer.. with a little bit of more understanding. Thanks for making me read :) lock(obj) is a CLR construct that for (intra-object?) thread synchronization. Ensures that only one thread can take ownership of the object's lock & enter the locked block of code. Other threads must wait till the current owner

Conditional Variable vs Semaphore

痞子三分冷 提交于 2019-11-26 21:11:38
When should one use a semaphore and when should one use a conditional variable (CondVar) ? Locks are used for mutual exclusion. When you want to ensure that a piece of code is atomic, put a lock around it. You could theoretically use a binary semaphore to do this, but that's a special case. Semaphores and condition variables build on top of the mutual exclusion provide by locks and are used for providing synchronized access to shared resources. They can be used for similar purposes. A condition variable is generally used to avoid busy waiting (looping repeatedly while checking a condition)

What is the difference between synchronized and static synchronized?

99封情书 提交于 2019-11-26 20:51:58
问题 For a travel booking web application, where there are 100 concurrent users logged in, should ticket booking and generating an "E-Ticket Number" be implemented by a "synchronized" or a "static synchronized" method? 回答1: Well, are you aware of the difference between a static method and an instance method in general? The only difference that synchronized makes is that before the VM starts running that method, it has to acquire a monitor. For an instance method, the lock acquired is the one