semaphore

C++0x has no semaphores? How to synchronize threads?

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it true that C++0x will come without semaphores? There are already some questions on Stack Overflow regarding the use of semaphores. I use them (posix semaphores) all the time to let a thread wait for some event in another thread: void thread0(...) { doSomething0(); event1.wait(); ... } void thread1(...) { doSomething1(); event1.post(); ... } If I would do that with a mutex: void thread0(...) { doSomething0(); event1.lock(); event1.unlock(); ... } void thread1(...) { event1.lock(); doSomethingth1(); event1.unlock(); ... } Problem: It's

Guaranteed semaphore order?

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: The documentation for the .NET Semaphore class states that: There is no guaranteed order, such as FIFO or LIFO, in which blocked threads enter the semaphore. In this case, if I want a guaranteed order (either FIFO or LIFO), what are my options? Is this something that just isn't easily possible? Would I have to write my own Semaphore? I assume that would be pretty advanced? Thanks, Steve 回答1: See this : The FifoSemaphore works exactly like a normal Semaphore but also guarantees that tokens are served out to acquirers in the order

Creating a method to perform animations and wait for completion using a semaphore in objective c

匿名 (未验证) 提交于 2019-12-03 01:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to create a method which makes use of UIView's "+animateWithDuration:animations:completion" method to perform animations, and wait for completion. I am well aware that I could just place the code that would normally come after it in a completion block, but I would like to avoid this because there is a substantial amount of code after it including more animations, which would leave me with nested blocks. I tried to implement this method as below using a semaphore, but I don't think this is the best way to do it, especially because

基于信号量的有界缓存

匿名 (未验证) 提交于 2019-12-03 00:28:02
1、使用信号量Semaphore来实现有界缓存 2、代码如下: /** * BoundedBuffer * <p/> * Bounded buffer using \Semaphore * * @author Brian Goetz and Tim Peierls */ @ThreadSafe public class SemaphoreBoundedBuffer < E > { private final Semaphore availableItems , availableSpaces ; @GuardedBy ( "this" ) private final E [] items ; @GuardedBy ( "this" ) private int putPosition = 0 , takePosition = 0 ; public SemaphoreBoundedBuffer ( int capacity ) { if ( capacity <= 0 ) throw new IllegalArgumentException () ; availableItems = new Semaphore ( 0 ) ; availableSpaces = new Semaphore ( capacity ) ; items = ( E []) new Object [

多线程并发访问之 Semaphore、CountDownLatch

匿名 (未验证) 提交于 2019-12-03 00:22:01
今天业务需求开发需要开发一个洗数据的小功能,大致业务是有百万级别的数据需要清洗,需要开发一个小功能,循环遍历百万数据调用一个服务接口清晰数据。考虑到接口的并发量,访问量不能太大, 整了一两个小时做了一个并发控制的小程序。 public static class CleanTask implements Runnable { private static Semaphore semaphore = new Semaphore( 5 , true ) ; private Runnable runnable ; private CountDownLatch latch ; public CleanTask (CountDownLatch latch , Runnable runnable) { this . runnable = runnable ; this . latch = latch ; } @Override public void run () { try { semaphore .acquire() ; runnable .run() ; semaphore .release() ; } catch (InterruptedException ex) { LOG .error( "线程中断异常" , ex) ; } finally { latch .countDown()

多线程学习(八)Semaphore学习

匿名 (未验证) 提交于 2019-12-03 00:22:01
Semaphore是计数信号量,它管理了一定数量的许可证,每个线程要使用资源必须获得许可证,使用完了再释放许可证。当许可证被发放完了时,其他线程再想获得,必须等待,即进入了阻塞状态,当有线程释放了许可证时,它才能获得执行。 方法摘要 acquire () 中断 。 中断 。 acquireUninterruptibly () availablePermits () drainPermits () getQueuedThreads () getQueueLength () hasQueuedThreads () isFair () true 。 release () toString () tryAcquire () 中断 ,则从此信号量获取给定数目的许可。 中断 ,则从此信号量获取一个许可。 比如某家餐厅只有五个餐桌,来了一百个人,每个人都想独占一张餐桌,这是就必须要等待有人吃完释放了餐桌,其他人才能吃,每次也只能有五个人同时吃,因为只有五张餐桌。 public class SemaphoreTest { public static void main(String[] args) { Semaphore semaphore = new Semaphore(5); for (int i = 0; i < 100; i++) { new Thread(new Runnable() {

并发工具的使用以及原理

匿名 (未验证) 提交于 2019-12-03 00:00:02
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("begin -ConditionDemoWait");     ry {       lock.lock();       condition.await();       System.out.println("end -ConditionDemoWait");     } catch (InterruptedException e) {       e.printStackTrace();     }finally {       lock.unlock();     }  } } ConditionSignal public class ConditionDemoSignal

Semaphore: 信号量

本秂侑毒 提交于 2019-12-02 23:37:57
Semaphore: 信号量 Semaphore: 可以指定多个线程同时访问某一资源。 一)、构造方法 //int permits:线程的准入数,即一个资源同时可以允许多少个线程访问 Semaphore semaphore = new Semaphore(int permits); //boolean fair,指明锁的规则, false: 非公平锁, true: 公平锁 Semaphore semaphore = new Semaphore(int permits, boolean fair) 公平锁: 锁的顺序与线程的执行顺序有关 非公平锁:锁的执行顺序与线程的执行顺序无关 默认使用非公平锁 。 二)、获取许可的方法 1)、acquire():获取一个许可,如果获取失败,则线程等待,等待期间可以响应中 断。 public void acquire() throws InterruptedException { //1.获取共享可中断的 sync.acquireSharedInterruptibly(1); } public final void acquireSharedInterruptibly(int arg) throws InterruptedException { //2.优先判断线程的中断状态 if (Thread.interrupted()) throw new

Semaphore

痞子三分冷 提交于 2019-12-02 23:37:39
public class Semaphore implements java.io.Serializable { private static final long serialVersionUID = -3222578661600680210L; /** All mechanics via AbstractQueuedSynchronizer subclass */ private final Sync sync; /** * 继承AQS以实现信号量机制,使用AQS的state属性代表允许使用的信号量的数量,该Sync类有两个 * 派生类,其中一个是公平模式的,另一个是非公平模式的 */ abstract static class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 1192457210091910933L; Sync(int permits) { setState(permits); } final int getPermits() { return getState(); } //非公平模式的获取信号量 final int nonfairTryAcquireShared(int acquires) { for (;;) { int

How does semaphore work?

巧了我就是萌 提交于 2019-12-02 23:12:35
Can the semaphore be lower than 0? I mean, say I have a semaphore with N=3 and I call "down" 4 times, then N will remain 0 but one process will be blocked? And same the other way, if in the beginning I call up, can N be higher than 3? Because as I see it, if N can be higher than 3 if in the beginning I call up couple of times, then later on I could call down more times than I can, thus putting more processes in the critical section then the semaphore allows me. If someone would clarify it a bit for me I will much appreciate. Greg Calling down when it's 0 should not work. Calling up when it's 3