synchronization

Synchronising N sibling processes after fork

流过昼夜 提交于 2019-11-27 07:24:46
问题 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

Spinlock with XCHG

别等时光非礼了梦想. 提交于 2019-11-27 07:07:31
问题 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

Deadlocks and Synchronized methods

笑着哭i 提交于 2019-11-27 07:04:52
问题 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

Synchronized functions using PHP

假如想象 提交于 2019-11-27 06:50:07
问题 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 回答1: 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

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

ε祈祈猫儿з 提交于 2019-11-27 06:40:41
问题 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

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

回眸只為那壹抹淺笑 提交于 2019-11-27 06:35:30
问题 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 回答1: 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

How pthread_mutex_lock is implemented

霸气de小男生 提交于 2019-11-27 06:33:44
I am just curious to know how functions related to synchronization between threads are implemented inside Unix. For example, what happens when I call pthread_mutex_lock ? Are there any pointers in use? A reference to the source code would really help. It is both complicated and differs from Unix to Unix variant. In Linux, for example, a system called Futex (Short for Fast Userspace Mutex) is used. In this system an atomic increment and test operation is performed on the mutex variable in user space. If the result of the operation indicates that there was no contention on the lock, the call to

How to synchronize two Subversion repositories?

别来无恙 提交于 2019-11-27 06:26:56
My company has a subsidiary with a slow Internet connection. Our developers there suffer to interact with our central Subversion server. Is it possible to configure a slave/mirror for them? They would interact locally with the server and all the commits would be automatically synchronized to the master server. This should work as transparently as possible for the developers. Usability is a must. Please, no suggestions to change our version control system. Subversion 1.5 introduced proxy support when you're using http to host your repository. Developers can checkout their working copies from

When is a condition variable needed, isn't a mutex enough?

非 Y 不嫁゛ 提交于 2019-11-27 06:23:11
I'm sure mutex isn't enough that's the reason the concept of condition variables exist; but it beats me and I'm not able to convince myself with a concrete scenario when a condition variable is essential. Differences between Conditional variables, Mutexes and Locks question's accepted answer says that a condition variable is a lock with a "signaling" mechanism. It is used when threads need to wait for a resource to become available. A thread can "wait" on a CV and then the resource producer can "signal" the variable, in which case the threads who wait for the CV get notified and can continue

How to synchronize or lock upon variables in Java?

非 Y 不嫁゛ 提交于 2019-11-27 06:17:54
Let me use this small and simple sample: class Sample { private String msg = null; public void newmsg(String x){ msg = x; } public String getmsg(){ String temp = msg; msg = null; return temp; } } Let's assume the function newmsg() is called by other threads that I don't have access to. I want to use the synchonize method to guarantee that the string msg is only used by one function per time. In other words, function newmsg() cannot run at the same time as getmsg() . That's pretty easy: class Sample { private String message = null; private final Object lock = new Object(); public void