semaphore

How to properly use PHP5 semaphores?

徘徊边缘 提交于 2019-11-29 14:43:09
I have this function that tries to read some values from cache. But if value does not exists it should call alternative source API and save new value into the cache. However, server is very overloaded and almost each time when value does not exists more then one requests are created (a lot of API calls) and each of them will store new vale into cache. However, what I want is to be able to call API many times, but only one process/request to be able to store it in cache: function fetch_cache($key, $alternativeSource) { $redis = new Redis(); $redis->pconnect(ENV_REDIS_HOST); $value = $redis->get

Interprocess semaphores sometimes not working as expected

走远了吗. 提交于 2019-11-29 14:40:27
I have the following C code, where variables prefixed by sm are shared by two processes proc1 and proc2 . Therefore the semaphores are also shared. This code is called repeatedly. So if I say previous value, that means value of a previous iteration. I notice in my program that proc1 sometimes passes sem_wait( sem_f2l ) without proc2 executing sem_post( sem_f2l ) . This I notice because sm_value_proc1 and sm_value_proc2 are supposed to be of same value in my program, which they are, as also confirmed by the printfs with >>> . However, the printf with <<< sometimes show different values. The

Semaphores on Python

帅比萌擦擦* 提交于 2019-11-29 11:59:51
问题 I've started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what I've got: import threading sem = threading.Semaphore() def fun1(): while True: sem.acquire() print(1) sem.release() def fun2(): while True: sem.acquire() print(2) sem.release() t = threading.Thread(target = fun1) t.start() t2 = threading.Thread(target = fun2) t2.start() But it keeps printing just 1's. How can I intercale the prints? 回答1: It

JUC 一 CyclicBarrier 与 Semaphore

空扰寡人 提交于 2019-11-29 10:19:00
java.util.concurrent CyclicBarrier简介 CyclicBarrier:可重用屏障/栅栏 类似于 CountDownLatch(倒计数闭锁),它能阻塞一组线程直到某个事件的发生。 与闭锁的关键区别在于,所有的线程必须同时到达屏障位置,才能继续执行。 CountDownLatch 的计数器只能使用一次,而 CyclicBarrier 的计数器可以使用 reset() 方法重置 CountDownLatch 采用减计数方式,CyclicBarrier 采用加计数方式 CyclicBarrier源码 构造函数: //线程到达屏障时,优先执行 barrierAction public CyclicBarrier(int parties, Runnable barrierAction) { if (parties <= 0) throw new IllegalArgumentException(); this.parties = parties; this.count = parties; this.barrierCommand = barrierAction; } public CyclicBarrier(int parties) { this(parties, null); } await(),await(long timeout, TimeUnit

What does mutex and semaphore actually do?

痴心易碎 提交于 2019-11-29 09:48:04
问题 I want some clarification regarding mutex and semaphore. My question is, What mutex actually do when a thread tries to enter a region locked by a mutex, a. it waits for the lock to be released? or b. it goes to sleep until the lock is released. In that case how it is wake up again when the lock is released? Same question as 1, but in this case it is semaphore. Can you give me some code regarding busy waiting in pthread in C, and also a case where thread goes to sleep instead of waiting? does

并发工具的使用以及原理

泄露秘密 提交于 2019-11-29 08:37:25
线程这块的一些工具类,基本都会以原理为主,通过分析别人代码的设计和实现,给自己提供积累一些方法和工具。 Condition 在前面学习 synchronized 的时候,有讲到 wait/notify 的 基本使用,结合 synchronized 可以实现对线程的通信。那 么这个时候我就在思考了,既然 J.U.C 里面提供了锁的实现 机制,那 J.U.C 里面有没有提供类似的线程通信的工具呢? 发现了一个 Condition 工具类。 Condition 是一个多线程协调通信的工具类,可以让某些线 程一起等待某个条件(condition),只有满足条件时,线程 才会被唤醒 Condition 的基本使用 ConditionWait public class ConditionDemoWait implements Runnable{   private Lock lock;  private Condition condition;   public ConditionDemoWait(Lock lock,   Condition condition){     this.lock=lock;     this.condition=condition;   }   @Override   public void run() {     System.out.println(

Is there any way to “wait here…” in code - just like an empty loop?

雨燕双飞 提交于 2019-11-29 07:46:57
Consider this code: [self otherStuff]; // "wait here..." until something finishes while(!self.someFlag){} [self moreStuff]; Note that this all happens ON THE SAME THREAD - we do not want to go to another thread. otherStuff could do things like connect to the cloud, get input from the user, etc. so it would take a lot of time and could follow many possible paths. otherStuff would set self.someFlag to true, when otherStuff is finally finished. This works perfectly and there's no problem with it at all -- except that it's lame to burn up the processor like that with the empty loop!! Quite simply,

How to protect a global variable shared by isr and regular function?

坚强是说给别人听的谎言 提交于 2019-11-29 07:19:21
Let's say I have function 1 and an isr routine , both share and update the same flag without any lock between them. the system is single threaded. the while will be a 3 arm assembly instructions, which means it is not atomic operation, is it ok to share a global variable between non isr and isr functions without any lock or protection? function 1: while (flag == false); flag = false; isr routine: do something flag=true I don't remember there is a linux kernel mechanism for locking between sleepable and non sleepable context e.g. irq and kernel thread . Thanks @artless for his answer here are

Performance test: sem_t v.s. dispatch_semaphore_t and pthread_once_t v.s. dispatch_once_t

二次信任 提交于 2019-11-29 06:56:02
问题 I wanted to know what would be better/faster to use POSIX calls like pthread_once() and sem_wait() or the dispatch_* functions, so I created a little test and am surprised at the results (questions and results are at the end). In the test code I am using mach_absolute_time() to time the calls. I really don’t care that this is not exactly matching up with nano-seconds; I am comparing the values with each other so the exact time units don't matter, only the differences between the interval do.

How to initialise a binary semaphore in C

孤人 提交于 2019-11-29 06:50:13
In the man page it appears that even if you initialise a semaphore to a value of one: sem_init(&mySem, 0, 1); It could still be incremented to a value greater than 1 with multiple calls to sem_post(&mySem); But in this code example the comment seems to think differently: sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */ Is it possible to initialise a strictly binary semaphore in C? Note: The reason for doing this instead of using a mutex in this case is the sem_post and sem_wait may be called by different threads. If you want a strictly binary semaphore on Linux, I suggest