semaphore

What does mutex and semaphore actually do?

与世无争的帅哥 提交于 2019-11-30 07:12:17
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 sleep mean it is blocked or sleeping is another kind of busy waiting? i want to know some programs

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

白昼怎懂夜的黑 提交于 2019-11-30 06:45:57
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. The numbers in the results section are repeatable and not averaged; I could have averaged the times but

objective-c : @synchronized, how does it work?

假如想象 提交于 2019-11-30 06:41:50
i have two methods -(void) a { @synchronized(self) { // critical section 1 } } -(void) b { @synchronized(self) { // critical section 2 } } now my question is if a thread is in critical section 1. will the critical section 2 be locked for other threads or other threads can access critical section 2. Critical section 2 will be blocked to other threads, as well, since you're synchronizing on the same object ( self ). 来源: https://stackoverflow.com/questions/2810459/objective-c-synchronized-how-does-it-work

Difference between Mutex, Semaphore & Spin Locks

烈酒焚心 提交于 2019-11-30 06:11:56
问题 I am doing experiments with IPC, especially with Mutex, Semaphore and Spin Lock. What I learnt is Mutex is used for Asynchronous Locking (with sleeping (as per theories I read on NET)) Mechanism, Semaphore are Synchronous Locking (with Signaling and Sleeping) Mechanism, and Spin Locks are Synchronous but Non-sleeping Mechanism. Can anyone help me to clarify these stuff deeply? And another doubt is about Mutex, when I wrote program with thread & mutex, while one thread is running another

Mutual exclusion and semaphores

南笙酒味 提交于 2019-11-30 05:06:41
I am writing a program (for homework) that simulates a unisex bathroom. Only 4 people are allowed at a time and men and woman cannot enter if the other sex is already using the bathroom. My problem is with allowing a max of 4 people in the bathroom. As you can see from the output, only 1 person is getting into the restroom at a time. Here is my code: const int Delayx = 60; int i; int restroom = 0; int Menwaiting = 0; int Womenwaiting = 0; semaphore max_capacity; semaphore woman; semaphore man; semaphore mutex; semaphore restroomcount; void Delay(void) { int DelayTime; DelayTime = random(Delayx

Named semaphores in Python?

偶尔善良 提交于 2019-11-30 02:39:06
问题 I have a script in python which uses a resource which can not be used by more than a certain amount of concurrent scripts running. Classically, this would be solved by a named semaphores but I can not find those in the documentation of the multiprocessing module or threading . Am I missing something or are named semaphores not implemented / exposed by Python? and more importantly, if the answer is no, what is the best way to emulate one? Thanks, Boaz PS. For reasons which are not so relevant

信号量Semaphore

孤者浪人 提交于 2019-11-30 01:01:48
信号量Semaphore 同进程的一样 Semaphore管理一个内置的计数器, 每当调用acquire()时内置计数器-1; 调用release() 时内置计数器+1; 计数器不能小于0;当计数器为0时,acquire()将阻塞线程直到其他线程调用release()。 from threading import Thread,currentThread,Semaphore import time def task(): sm.acquire() print(f'{currentThread().name}在执行') time.sleep(3) sm.release() sm=Semaphore(5) for i in range(15): t=Thread(target=task) t.start() 来源: https://www.cnblogs.com/aden668/p/11542639.html

Semaphore CountDownLatch CyclicBarrier 源码分析

血红的双手。 提交于 2019-11-29 23:19:38
java5 中 ,提供了几个并发工具类 ,Semaphore CountDownLatch CyclicBarrier,在并发编程中非常实用。前两者通过 内部类sync 继承AQS,使用共享资源的模式,AQS的实现可参考我的另一篇 AQS 实现分析 ,前两者根据各自功能需求 , 各自内部实现tryAcquireShared(获取资源)、tryReleaseShared(释放)。来定义什么条件 下来获取与释放。而CyclicBarrier内部通过Reentrantlock与Condition组合的方式实现。 Semaphore 与Reentrantlock类似,也有公平和非公平的机制。这里就不在分析了,默认是非公平的。 通过acquire 获取 Semphore public void acquire() throws InterruptedException { sync.acquireSharedInterruptibly(1); //非独占模式 } AQS public final void acquireSharedInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if

Need to understand the usage of SemaphoreSlim

梦想与她 提交于 2019-11-29 20:25:14
Here is the code I have but I don't understand what SemaphoreSlim is doing. async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); List<Task> trackedTasks = new List<Task>(); while (DoMore()) { await ss.WaitAsync(); trackedTasks.Add(Task.Run(() => { DoPollingThenWorkAsync(); ss.Release(); })); } await Task.WhenAll(trackedTasks); } void DoPollingThenWorkAsync() { var msg = Poll(); if (msg != null) { Thread.Sleep(2000); // process the long running CPU-bound job } } What does await ss.WaitAsync(); & ss.Release(); do? I guess that if I run 50 threads at a time then write code

“Block” main thread (dispatch_get_main_queue()) and (or not) run currentRunLoop periodically - what is the difference?

帅比萌擦擦* 提交于 2019-11-29 16:02:50
问题 I have the following code: - (void)test_with_running_runLoop { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); NSTimeInterval checkEveryInterval = 0.05; NSLog(@"Is main queue? : %d", dispatch_get_current_queue() == dispatch_get_main_queue()); dispatch_async(dispatch_get_main_queue(), ^{ sleep(1); NSLog(@"I will reach here, because currentRunLoop is run"); dispatch_semaphore_signal(semaphore); }); while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) [[NSRunLoop