semaphore

Ruby Semaphores?

烂漫一生 提交于 2019-12-03 15:48:39
问题 I'm working on an implementation of the "Fair Barbershop" problem in Ruby. This is for a class assignment, but I'm not looking for any handouts. I've been searching like crazy, but I cannot seem to find a Ruby implementation of Semaphores that mirror those found in C. I know there is Mutex, and that's great. Single implementation, does exactly what that kind of semaphore should do. Then there's Condition Variables. I thought that this was going to work out great, but looking at these, they

Why does the acquire() method in Semaphores not have to be synchronized?

元气小坏坏 提交于 2019-12-03 14:59:00
I am getting into Semaphores in Java and was reading this article http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html . The only thing I don't get is why the acquire() method is not used in a synchronized context. Looking at the example from the above webiste: They create a semaphore: private Semaphore semaphore = new Semaphore(100); and get a permit just like this: semaphore.acquire(); Now, wouldn't it be possible that two or more threads try to acquire() at the same time? If so, there would be a little problem with the count. Or, does the semaphore itself handle

How and why can a Semaphore give out more permits than it was initialized with?

你。 提交于 2019-12-03 11:53:43
I am reading the book Java Concurrency in Practice. In a section about java.util.concurrent.Semaphore , the below lines are present in the book. It is a comment about its implementation of "virtual permit" objects The implementation has no actual permit objects, and Semaphore does not associate dispensed permits with threads, so a permit acquired in one thread can be released from another thread. You can think of acquire as consuming a permit and release as creating one; a Semaphore is not limited to the number of permits it was created with. Can somebody explain this? I am having trouble

Locking with nested async calls

不问归期 提交于 2019-12-03 11:32:26
I am working on a multi threaded WindowsPhone8 app that has critical sections within async methods. Does anyone know of a way to properly use semaphores / mutexes in C# where you are using nested async calls where the inner method may be acquiring the same lock that it already acquired up the callstack? I thought the SemaphoreSlim might be the answer, but it looks like it causes a deadlock. public class Foo { SemaphoreSlim _lock = new SemaphoreSlim(1); public async Task Bar() { await _lock.WaitAsync(); await BarInternal(); _lock.Release(); } public async Task BarInternal() { await _lock

Java多线程(十五):CountDownLatch,Semaphore,Exchanger,CyclicBarrier,Callable和Future

你说的曾经没有我的故事 提交于 2019-12-03 11:20:43
摘自: https://www.cnblogs.com/Java-Starter/p/11570249.html Java多线程(十五):CountDownLatch,Semaphore,Exchanger,CyclicBarrier,Callable和Future 技术讨论QQ群:135202158 目录 CountDownLatch Semaphore Exchanger CyclicBarrier Callable和Future 正文 回到顶部 CountDownLatch CountDownLatch用来使一个线程或多个线程等待到其他线程完成。CountDownLatch有个初始值count,await方法会阻塞线程,直到通过countDown方法调用使count减少为0才会执行await方法后面的代码。 示例代码 MyThread50_0是WorkThread,不同的线程休眠时间不一样。 public class MyThread50_0 extends Thread { private CountDownLatch cdl; private int sleepSecond; public MyThread50_0(String name, CountDownLatch cdl, int sleepSecond) { super(name); this.cdl = cdl

Semaphore and synchronization

帅比萌擦擦* 提交于 2019-12-03 11:17:39
I could not quite understand the following from the semaphore description in javadocs. Note that no synchronization lock is held when acquire() is called as that would prevent an item from being returned to the pool. The semaphore encapsulates the synchronization needed to restrict access to the pool, separately from any synchronization needed to maintain the consistency of the pool itself. Can someone please help me understand this and its implications. A semaphore acts as a limiter of available resource pool depth; for example, a semaphore with a capacity of 10 allows a maximum of 10 threads

dispatch_semaphore_wait does not wait on semaphore

匿名 (未验证) 提交于 2019-12-03 08:59:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have developed the following method, which checks the app's ability to communicate with the server. The method performs a simple query and knows that if it gets a result, the app should be connected (basic ping mechanism). - (BOOL)isAppConnected { __block BOOL isConnected = NO; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[SFRestAPI sharedInstance] performSOQLQuery:@"SELECT id FROM Account LIMIT 1" failBlock:^(NSError *e) { isConnected = NO; NSLog(@"NOT CONNECTED %@", e); NSLog(@"fail block ON THE MAIN THREAD? %hhd",

TPL Dataflow vs plain Semaphore

强颜欢笑 提交于 2019-12-03 08:53:10
I have a requirement to make a scalable process. The process has mainly I/O operations with some minor CPU operations (mainly deserializing strings). The process query the database for a list of urls, then fetches data from these urls, deserilize the downloaded data to objects, then persist some of the data into crm dynamics and also to another database. Afterwards I need to update the first database which urls were processed. Part of the requirement is to make the parallelism degree configurable. Initially I thought to implement it via a sequence of tasks with await and limit the parallelism

Automatic Semaphore release on process Exit

落花浮王杯 提交于 2019-12-03 08:39:49
I am using Semaphore to limit the number of concurrent instances my application can run. There are many ways a process can terminate. Can the Semaphore be created so it automatically releases upon process exit? EDIT: I would like some magic to automatically clean up the semaphore 'raised' state for the process owning it upon exit or crash. Just to be sure that it is cleared, no matter what. MORE: I am looking for any viable option for it, considering: it would be great that NO external application is required to get a hold on every instance of the protected application it doesn't have to be

Piping data between threads with Java

亡梦爱人 提交于 2019-12-03 08:21:58
I am writing a multi-threaded application that mimics a movie theater. Each person involved is its own thread and concurrency must be done completely by semaphores. The only issue I am having is how to basically link threads so that they can communicate (via a pipe for instance). For instance: Customer[1] which is a thread, acquires a semaphore that lets it walk up to the Box Office. Now Customer[1] must tell the Box Office Agent that they want to see movie "X". Then BoxOfficeAgent[1] also a thread, must check to make sure the movie isn't full and either sell a ticket or tell Customer[1] to