semaphore

PHP no wait sem_acquire?

ⅰ亾dé卋堺 提交于 2019-12-10 17:30:08
问题 Not a specific code question, but more of a general coding question. I'm trying to use a semaphore in a work project to limit the number of users that can access certain processes at a concurrent time. From my understanding the following: $iKey = ftock($sSomeFileLocation,'sOneCharacterString'); //Generate the key if($sem_id = sem_get($iKey)){ //1 user allowed if(sem_acquire($sem_id)){ //Do the limited process here sem_release($sem_id); } } The problem that I see here is that if there is

Does using a lock have better performance than using a local (single application) semaphore? [closed]

岁酱吖の 提交于 2019-12-10 17:20:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 8 months ago . Does using a lock have better performance than using a local (single application) semaphore? I read this blog from msdn : Producer consumer solution on msdn and I didn't like their solution to the problem because there are always 20 elements left in the queue. So instead, I

Showing a simple example of deadlock with semaphores

ⅰ亾dé卋堺 提交于 2019-12-10 13:46:23
问题 I'm currently taking Operating Systems and our teacher assigned this problem for our lab but he's not very helpful. So I need to show a basic example of deadlock with semaphores and my output needs to demonstrate the occurence of the deadlock. I'm assuming he means if my exception is caught. This is as close as I've gotten. import java.util.concurrent.Semaphore; public class deadlockTest2 { private Semaphore sem1=new Semaphore(1); private Semaphore sem2=new Semaphore(1); private int num;

Fair semaphore in python

好久不见. 提交于 2019-12-10 13:24:37
问题 Is it possible to have a fair semaphore in python, one that guarantees that blocking threads are unblocked in the order they call acquire() ? 回答1: You might have to build one from other moving parts. For example, create a Queue.Queue() to which each listener posts a brand-new Event() on which it then waits. When it is time to wake up one of the waiting threads, pop off the item on the queue that has been waiting longest — it will be one of those event objects — and release the thread through

ABAddressBookRequestAccessWithCompletion iOS 7 and semaphores

倖福魔咒の 提交于 2019-12-10 10:17:40
问题 this code has been posted before, and been used as well, from what i could gather. i'm in a situation where i need the code to NOT continue until i know if i have access to the contacts. on Xcode 5.0.2 and iOS 6, this works just fine. on iOS 7, it hangs forever, and then when i kill the app the dialog box comes up asking to allow access to the contacts. ABAddressBookRef addressBook = ABAddressBookCreate(); __block BOOL accessGranted = NO; if (ABAddressBookRequestAccessWithCompletion != NULL)

how to give priority to the threads waiting in a semaphore?

自作多情 提交于 2019-12-10 06:40:55
问题 I have used a semaphore to restrict the number of threads accessing a function. I want the thread to be awakened next should be chosen by some priority which i will be giving,not by default way that semaphore awaken's them ? How can we achieve this ? Here is the implementation : class MyMathUtil2 implements Runnable { double a; double b; String name = "demo"; Thread t; //static int currentCount = 0; static int MAX_COUNT = 2; private final Semaphore available = new Semaphore(MAX_COUNT, true);

dispatch_semaphore_t reuse - What am I missing here?

二次信任 提交于 2019-12-10 03:37:57
问题 I have some code where I am using dispatch_semaphore_t to signal operation completion. When the semaphore is a member variable, it does not seem to behave correctly. I will show example code that works and an example that does not seem to work: @implementation someClass { dispatch_semaphore_t memberSem; dispatch_semaphore_t* semPtr; NSThread* worker; BOOL taskDone; } - (id)init { // Set up the worker thread and launch it - not shown here. memberSem= dispatch_semaphore_create(0); semPtr= NULL;

How do I prevent “maxing out” of CPU: Synchronous method calling multiple workers asynchronously & throttling using SemaphoreSlim?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 03:26:07
问题 I am currently optimizing an existing, very slow and timing out production application. There is no option to re-write it . In short, it is a WCF service that currently calls 4 other "worker" WCF services sequentially. None of the worker services are dependent on results from the other. So we would like it to call them all at once (not sequentially) . I will reiterate that we don't have the luxury of re-writing it. The optimization involves making it call all worker services at once. This is

Are Semaphore P and V operations atomic?

冷暖自知 提交于 2019-12-10 01:54:41
问题 Are the P() and V() operations that can be performed on a semaphore guarantee atomic? Can a semaphore prevent two processes getting into the P()? 回答1: Suppose we have a binary semaphore, s, which has the value 1, and two processes simultaneously attempt to execute P on s. Only one of these operations will be able to complete before the next V operation on s; the other process attempting to perform a P operation is suspended. Taken from my university notes: We can think if P and V as

sem_init(…): What is the pshared parameter for?

▼魔方 西西 提交于 2019-12-10 01:43:17
问题 In a graduate class, we've had to use semaphores to accomplish work with threads. We were directed to use sem_init along with a bunch of other sem_* procedure but we were not given much information about the details of each of these sem_* methods. The prototype (and header file) of sem_init is the following: #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); but I don't understand what the pshared value is used for. According to opengroup.org: If the pshared