synchronization

Behavior differences between performBlock: and performBlockAndWait:?

那年仲夏 提交于 2019-11-27 09:32:45
问题 I'm creating an NSManagedObjectContext in a private queue to handle data updates I take from files and/or services: NSManagedObjectContext *privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; privateContext.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator; Since I'm using a private queue, I don't fully understand the difference between performBlock:

What is the difference between lock and Mutex?

会有一股神秘感。 提交于 2019-11-27 09:18:28
问题 What is the difference between lock and Mutex? Why can't they be used interchangeably? 回答1: A lock is specific to the AppDomain, while Mutex to the Operating System allowing you to perform inter-process locking and synchronization (IPC). 回答2: lock is a compiler keyword, not an actual class or object. It's a wrapper around the functionality of the Monitor class and is designed to make the Monitor easier to work with for the common case. The Monitor (and the lock keyword) are, as Darin said,

Ensure Java synchronized locks are taken in order?

有些话、适合烂在心里 提交于 2019-11-27 09:15:53
we have two threads accessing one list via a synchronized method. Can we a) rely on the run time to make sure that each of them will receive access to the method based on the order they tried to or b) does the VM follow any other rules c) is there a better way to serialize the requests? No, synchronized will give access in any order (Depends on the JVM implementation). This could even cause Threads to starve in some scenarios. You can ensure the order by using ReentrantLock (since Java 5.0) with the fair=true option. ( Lock lock = new ReentrantLock(true); ) No you cannot be sure that two calls

Implementing a critical section in CUDA

南楼画角 提交于 2019-11-27 09:15:06
I'm trying to implement a critical section in CUDA using atomic instructions, but I ran into some trouble. I have created the test program to show the problem: #include <cuda_runtime.h> #include <cutil_inline.h> #include <stdio.h> __global__ void k_testLocking(unsigned int* locks, int n) { int id = threadIdx.x % n; while (atomicExch(&(locks[id]), 1u) != 0u) {} //lock //critical section would go here atomicExch(&(locks[id]),0u); //unlock } int main(int argc, char** argv) { //initialize the locks array on the GPU to (0...0) unsigned int* locks; unsigned int zeros[10]; for (int i = 0; i < 10; i++

pthreads mutex vs semaphore

你离开我真会死。 提交于 2019-11-27 09:11:21
问题 What is the difference between semaphores and mutex provided by pthread library ? 回答1: semaphores have a synchronized counter and mutex's are just binary (true / false). A semaphore is often used as a definitive mechanism for answering how many elements of a resource are in use -- e.g., an object that represents n worker threads might use a semaphore to count how many worker threads are available. Truth is you can represent a semaphore by an INT that is synchronized by a mutex. 回答2: I am

Java threads locking on a specific object

烂漫一生 提交于 2019-11-27 08:08:59
问题 I have a web application and I am using Oracle database and I have a method basically like this: public static void saveSomethingImportantToDataBase(Object theObjectIwantToSave) { if (!methodThatChecksThatObjectAlreadyExists) { storemyObject() //pseudo code } // Have to do a lot other saving stuff, because it either saves everything or nothing commit() // pseudo code to actually commit all my changes to the database. } Right now there is no synchronization of any kind so n threads can of

Equivalent of PostMessage in C# to synchronize with the main thread with MVVM?

此生再无相见时 提交于 2019-11-27 07:47:02
问题 I must be retarded with searching, because here's another seemingly common problem that I haven't been able to solve. Here's my problem -- I am using WPF and MVVM, and I have a statemachine that executes in the model. If an error occurs, I need to pass information up to the ViewModel to display the error. This part seems to work okay. When the user clicks the desired behavior, the code in the model continues, and looks at the object the user interacts with to determine what to do next. The

How to synch JavaScript callbacks?

我是研究僧i 提交于 2019-11-27 07:41:34
I've been developing in JavaScript for quite some time but net yet a cowboy developer, as one of the many things that always haunts me is synching JavaScript's callbacks. I will describe a generic scenario when this concern will be raised: I have a bunch of operations to perform multiple times by a for loop, and each of the operations has a callback. After the for loop, I need to perform another operation but this operation can only execute successfully if all the callbacks from the for loop are done. Code Example: for ... in ... { myFunc1(callback); // callbacks are executed asynchly }

Java Equivalent of .NET's ManualResetEvent and WaitHandle

送分小仙女□ 提交于 2019-11-27 07:37:16
问题 I would like to know if Java provides an equivalent of .NET's classes of ManualResetEvent and WaitHandle, as I would like to write code that blocks for a given timeout unless an event is triggered. The .NET classes of WaitHandle and ManualResetEvent provide a nice, hassle-free interface for that which is also thread-safe as far as I know, so what does Java has to offer? 回答1: Have you considered using wait / notify (the equivalent of Monitor.Wait and Monitor.Pulse ) instead? You'll want a

Guava Cache, how to block access while doing removal

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 07:25:21
问题 I have thread A, inserting a new element to Guava Cache, and because of the Size policy, the cache will evict element associated with key Y. Unfortunately, the removal process R of Y takes long, and during the time Y is being process by R (already evicted but still in R), there is another thread B trying to get data associated with key Y. Basically, R will try to update the database for the key Y, and while that value is not updated, thread B try to access the database for value associated