semaphore

Automatic Semaphore release on process Exit

夙愿已清 提交于 2019-12-04 13:39:46
问题 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

Implementing semaphore by using mutex operations and primitives

霸气de小男生 提交于 2019-12-04 13:13:18
问题 Some time ago had an interview and was asked to implement Semaphore by using mutex operations and primitives only (he allowed int to be considered as atomic). I came with solution below. He did not like busy/wait part -- while (count >= size) {} -- and asked to implement locking instead by using more primitive types and mutexes. I did not manage to come with improved solution. Any ideas how it could be done? struct Semaphore { int size; atomic<int> count; mutex updateMutex; Semaphore(int n) :

How to create Semaphore between HTML elements loaded async

别说谁变了你拦得住时间么 提交于 2019-12-04 12:01:34
问题 I have in an HTML page, an element that appears several times, and running the same JS. Problem is, I want it to do a specific function only if it was the first one to run it (his siblings never ran it - YET). I need semaphore to sync between them. I am unable to know how to declare a variable and to do semaphore in this way in JS. 回答1: There are lots of approaches. You need to put a flag somewhere . In the absence of anything else, you can put it on window , but use a name that's unlikely to

Allow only 3 instances of an application using semaphores

☆樱花仙子☆ 提交于 2019-12-04 11:58:52
问题 I am trying to implement a simple routine using semaphores that will allow me to run only 3 instances of the application. I could use 3 mutexes but that is not a nice approach i tried this so far var hSem:THandle; begin hSem := CreateSemaphore(nil,3,3,'MySemp3'); if hSem = 0 then begin ShowMessage('Application can be run only 3 times at once'); Halt(1); end; How can i do this properly ? 回答1: Always make sure that you release a semaphore because this is not done automatically if your

Piping data between threads with Java

隐身守侯 提交于 2019-12-04 11:51:15
问题 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

Parse crash when calling [PFFacebookUtils initializeFacebook] - semaphore_wait_trap

爱⌒轻易说出口 提交于 2019-12-04 11:44:30
问题 Since the latest Parse release (v1.6.3) my app gets stuck at launch, and the last breakpoint it hits is [PFFacebookUtils initializeFacebook]; If I hit pause and look at the debugger, the stack trace looks like this: I'm calling [PFFacebookUtils initializeFacebook] in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions as advised. From googling the semaphore_wait_trap issue, it seems to be related to clashing background threads(?) in

Synchronized Vs Semaphore

帅比萌擦擦* 提交于 2019-12-04 09:59:45
问题 While reading concurrency in Java, I have following doubts: Does Java provides lower level construct then synchronized for synchronization? In what circumstances will we use semaphore over synchronized (which provides monitor behaviour in Java) 回答1: Synchronized allows only one thread of execution to access the resource at the same time. Semaphore allows up to n (you get to choose n) threads of execution to access the resource at the same time. 回答2: There is also volatile keyword, according

三个线程abc顺序执行

[亡魂溺海] 提交于 2019-12-04 08:48:04
1.使用synchronized悲观锁 (秋招阿里的一个笔试题,应该写的比较复杂,然后就没有然后了o(╥﹏╥)o) public class ThreadThreadp { private int flag = 0; public synchronized void printa() throws InterruptedException { while (true) { if(flag ==0) { System.out.print("A"); flag = 1; notifyAll(); } wait(); } } public synchronized void printb() throws InterruptedException { while (true) { if(flag ==1) { System.out.print("B"); flag = 2; notifyAll(); } wait(); } } public synchronized void printc() throws InterruptedException { while (true) { if (flag == 2) { System.out.print("C"); Thread.sleep(1000); flag = 0; notifyAll(); } wait(); } } public

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

[亡魂溺海] 提交于 2019-12-04 07:38:18
Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time // the method the loop belongs to is declared synchronized threadA = new Thread(a); threadA.start(); a

Semaphore solution to reader-writer: order between updating reader count and waiting or signaling on read/write binary semaphore?

橙三吉。 提交于 2019-12-04 05:55:01
问题 From Operating System Concepts In the solution to the first readers–writers problem, the reader processes share the following data structures: semaphore rw mutex = 1; semaphore mutex = 1; int read_count = 0; do { wait(rw_mutex); . . . /* writing is performed */ . . . signal(rw_mutex); } while (true); Figure 5.11 The structure of a writer process. do { wait(mutex); read count++; if (read_count == 1) wait(rw mutex); signal(mutex); . . . /* reading is performed */ . . . wait(mutex); read count--;