semaphore

Javascript semaphore / test-and-set / lock?

a 夏天 提交于 2019-11-27 20:37:30
问题 Is there such a thing as an atomic test-and-set, semaphore, or lock in Javascript? I have javascript invoking async background processes via a custom protocol (the background process literally runs in a separate process, unrelated to the browser). I believe I'm running into a race condition; the background process returns between my test and my set, screwing things up on the javascript side. I need a test-and-set operation to make it a real semaphore. Here's the javascript code that attempts

Is there any mutex/semaphore mechanism in shell scripts?

那年仲夏 提交于 2019-11-27 20:27:55
I'm looking for mutex/semaphore/concurrency mechanism in shell script. Consider following situation: Unless "a" user does not close the shared file, "b" user should not able to open/update it. I'm just wondering how to implement mutex, semaphore, critical sections, etc. in shell scripting. Which is the easiest way to implement locking mechanism [file level] in shell scripting? See BashFAQ and ProcessManagment for discussions on file locking in Bash. Tagging your question as shell (only) limits the number of people that can help you. You may want to add unix, ksh, bash. There are numerous

Binary Semaphore vs a ReentrantLock

◇◆丶佛笑我妖孽 提交于 2019-11-27 20:07:25
问题 I've been trying to understand Reentrant locks and Semaphores ( the nesting of Reentrant locks vs release/unlock mechanism ). It seems that having a Semaphore requires you to write a more thoroughly tested application because the release() method does not check if the thread releasing the permit is actually holding it. When I tested my test code, I found out that this may subsequently increase the number of permits beyond the initial limit. On the other hand, if a thread is not holding a

In what situation do you use a semaphore over a mutex in C++?

≡放荡痞女 提交于 2019-11-27 19:58:01
问题 Throughout the resources I've read about multithreading, mutex is more often used and discussed compared to a semaphore. My question is when do you use a semaphore over a mutex? I don't see semaphores in Boost thread. Does that mean semaphores no longer used much these days? As far as I've understand, semaphores allow a resource to be shared by several threads. This is only possible if those threads are only reading the resource but not writing. Is this correct? 回答1: Boost.Thread has mutexes

Java并发编程:CountDownLatch、CyclicBarrier和Semaphore

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 19:29:20
一.CountDownLatch用法   CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 CountDownLatch类只提供了一个构造器: public CountDownLatch(int count) { }; //参数count为计数值 然后下面这3个方法是CountDownLatch类中最重要的方法: public void await() throws InterruptedException { }; //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { }; //和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行 public void countDown() { }; //将count值减1 下面看一个例子大家就清楚CountDownLatch的用法了: public class Test { public static void main(String[]

Run one instance from the application

泄露秘密 提交于 2019-11-27 16:57:02
问题 I have a windows application (C#) and i need to configure it to run one instance from the application at the time , It means that one user clicked the .exe file and the application is run and the user didn't close the first instance of the application that is being run and need to run a next instance so it should appear the first instance and not opening new one. can any one help me how to do that? thanks in advance 回答1: I often solve this by checking for other processes with the same name.

.NET Reverse Semaphore?

末鹿安然 提交于 2019-11-27 15:42:06
问题 Perhaps it's too late at night, but I can't think of a nice way to do this. I've started a bunch of asynchronous downloads, and I want to wait until they all complete before the program terminates. This leads me to believe I should increment something when a download starts, and decrement it when it finishes. But then how do I wait until the count is 0 again? Semaphores sort of work in the opposite way in that you block when there are no resources available, not when they're all available

Linux synchronization with FIFO waiting queue

拟墨画扇 提交于 2019-11-27 14:13:54
问题 Are there locks in Linux where the waiting queue is FIFO? This seems like such an obvious thing, and yet I just discovered that pthread mutexes aren't FIFO, and semaphores apparently aren't FIFO either (I'm working on kernel 2.4 (homework))... Does Linux have a lock with FIFO waiting queue, or is there an easy way to make one with existing mechanisms? 回答1: Here is a way to create a simple queueing "ticket lock", built on pthreads primitives. It should give you some ideas: #include <pthread.h>

semaphore implementation

大憨熊 提交于 2019-11-27 13:19:22
问题 I am getting error in the following program. I want to demonstrate how two processes can share a variable using semaphore. Can anyone guide me? I am not able to debug the errors... #include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<sys/ipc.h> #include<sys/sem.h> #include<semaphore.h> int main() { int pid,mutex=1; int semid; /* semid of semaphore set */ key_t key = 1234; /* key to pass to semget() */ int nsems = 1; /* nsems to pass to semget() */ semid=semget(key,nsems,IPC_CREAT

Difference between Counting and Binary Semaphores

百般思念 提交于 2019-11-27 11:56:14
What is the difference between Counting and binary semaphore. What I have seen somewhere is that both can control N number of processes which have requested for a resource. Both have taken and Free states. Is there any restriction on how many Resources a Binary semaphore and Counting semaphore can protect? Both allow only One process to use a resource at a time... Is there any other difference? Are the above mentioned properties correct? Actually, both types are used to synchronize access to a shared resource, whether the entity which is trying to access is a process or even a thread. The