synchronization

does Monitor.Wait Needs synchronization?

自古美人都是妖i 提交于 2019-11-28 13:35:36
I have developed a generic producer-consumer queue which pulses by Monitor in the following way: the enqueue : public void EnqueueTask(T task) { _workerQueue.Enqueue(task); Monitor.Pulse(_locker); } the dequeue: private T Dequeue() { T dequeueItem; if (_workerQueue.Count > 0) { _workerQueue.TryDequeue(out dequeueItem); if(dequeueItem!=null) return dequeueItem; } while (_workerQueue.Count == 0) { Monitor.Wait(_locker); } _workerQueue.TryDequeue(out dequeueItem); return dequeueItem; } the wait section produces the following SynchronizationLockException : "object synchronization method was called

Java Equivalent of .NET's ManualResetEvent and WaitHandle

北城以北 提交于 2019-11-28 13:20:46
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? Have you considered using wait / notify (the equivalent of Monitor.Wait and Monitor.Pulse ) instead? You'll want a little bit of checking to see whether you actually need to wait (to avoid race conditions) but it should work.

Synchronising N sibling processes after fork

a 夏天 提交于 2019-11-28 13:06:32
I'm having some hard time with synchronising N child process waiting each one of them to arrive at some specific point. I've tried semaphores and signals but I can't get my head around it. #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/types.h> #include <sys/sem.h> #include <sys/ipc.h> #include <fcntl.h> #include <semaphore.h> #include <sys/wait.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/msg.h> #define NUM_KIDS 4 void handle(int signum); int main(int argc, char const *argv[]) { sem_t* sem;

Guava Cache, how to block access while doing removal

只愿长相守 提交于 2019-11-28 13:02:59
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 with key Y, which is still the old value. Question is: how can I block thread B from accessing element

How does Firebase sync work, with shared data?

喜欢而已 提交于 2019-11-28 12:40:12
I use Firebase to handle the Auth topic of my Android app. I also save a user profile on Firebase, that contain user id and extra options that user can update in the android app. At startup, the app check the auth, and auth. It reload the user profile (on Firebase) to then update my userInstance on the app. Firebase is set as offline capable at app level. The userInstance POJO is sync with Firebase at log and stop to be sync at unlog. I have a special parameter, in my user profile that I can change (as an admin). Every-time I update it on the Firebase console, it is replaced by the previous

Spinlock with XCHG

人盡茶涼 提交于 2019-11-28 12:33:26
The example implementation Wikipedia provides for a spinlock with the x86 XCHG command is: ; Intel syntax locked: ; The lock variable. 1 = locked, 0 = unlocked. dd 0 spin_lock: mov eax, 1 ; Set the EAX register to 1. xchg eax, [locked] ; Atomically swap the EAX register with ; the lock variable. ; This will always store 1 to the lock, leaving ; the previous value in the EAX register. test eax, eax ; Test EAX with itself. Among other things, this will ; set the processor's Zero Flag if EAX is 0. ; If EAX is 0, then the lock was unlocked and ; we just locked it. ; Otherwise, EAX is 1 and we didn

Deadlocks and Synchronized methods

南笙酒味 提交于 2019-11-28 12:28:44
I've found one of the code on Stack Overflow and I thought it is pretty similar to what I am facing but I still don't understand why this would enter a deadlock. The example was taken from Deadlock detection in Java : Class A { synchronized void methodA(B b) { b.last(); } synchronized void last() { System.out.println(“ Inside A.last()”); } } Class B { synchronized void methodB(A a) { a.last(); } synchronized void last() { System.out.println(“ Inside B.last()”); } } Class Deadlock implements Runnable { A a = new A(); B b = new B(); // Constructor Deadlock() { Thread t = new Thread(this); t

Synchronized functions using PHP

ぃ、小莉子 提交于 2019-11-28 12:05:46
How to make functions in PHP synchronized so that same function won't be executed concurrently ? 2nd user must wait till 1st user is done with the function. Then 2nd user can execute the function. Thanks This basically comes down to setting a flag somewhere that the function is locked and cannot be executed until the first caller returns from that function. This can be done in a number of ways: use a lock file (first function locks a file name "f.lok", second function checks if the lock file exists and executes or doesn't based on that evaluation) set a flag in the database (not recomended)

Do not share same socket between two threads at the same time

假装没事ソ 提交于 2019-11-28 12:01:12
I have around 60 sockets and 20 threads and I want to make sure each thread works on different socket everytime so I don't want to share same socket between two threads at all. In my SocketManager class, I have a background thread which runs every 60 seconds and calls updateLiveSockets() method. In the updateLiveSockets() method, I iterate all the sockets I have and then start pinging them one by one by calling send method of SendToQueue class and basis on the response I mark them as live or dead. In the updateLiveSockets() method, I always need to iterate all the sockets and ping them to

Synchronizing on an object in java, then changing the value of the synchronized-on variable

自作多情 提交于 2019-11-28 11:54:39
I came across a code like this synchronized(obj) { obj = new Object(); } Something does not feel right about this , I am unable to explain, Is this piece of code OK or there is something really wrong in it, please point it out. Thanks It's probably not what you want to do. You're synchronizing on an object that you're no longer holding a reference to. Consider another thread running this method: they may enter and try to hit the lock at the moment after the reference to obj has been updated to point to the new object. At that point, they're synchronizing on a different object than the first