semaphore

Are “benaphores” worth implementing on modern OS's?

。_饼干妹妹 提交于 2019-12-11 08:49:46
问题 Back in my days as a BeOS programmer, I read this article by Benoit Schillings, describing how to create a "benaphore": a method of using atomic variable to enforce a critical section that avoids the need acquire/release a mutex in the common (no-contention) case. I thought that was rather clever, and it seems like you could do the same trick on any platform that supports atomic-increment/decrement. On the other hand, this looks like something that could just as easily be included in the

How to lock a long async call in a WebApi action?

 ̄綄美尐妖づ 提交于 2019-12-11 06:08:13
问题 I have this scenario where I have a WebApi and an endpoint that when triggered does a lot of work (around 2-5min). It is a POST endpoint with side effects and I would like to limit the execution so that if 2 requests are sent to this endpoint (should not happen, but better safe than sorry), one of them will have to wait in order to avoid race conditions. I first tried to use a simple static lock inside the controller like this: lock (_lockObj) { var results = await _service

Problem with using Semaphore to protect queue

拥有回忆 提交于 2019-12-11 06:01:07
问题 I am using following code to limit use of resources. Once in a while(after 3-4 days of successful run) I get queue empty exception or the returned object is found to be null. I am wondering if I am limiting only 5 threads to enter this Get method, how come this happens. The places where GetConnection is called, ReleaseConnection is also definitely called within the Finally block. With each call, I am also logging no. of resources in the queue. The queue count never seems to be going more than

Using volatile variables and semaphores - Java

。_饼干妹妹 提交于 2019-12-11 05:17:16
问题 I'm starting with Threads, Semaphores, volatile variables, etc. I wonder if when I'm using Semaphores it is necessary to define the variable as volatile, I mean: Having 2 Threads, one increases and the other decreases the variable for example and obviously, before each access I have a mutex that controls at any time only one thread is "playing" with the variable. It would be necessary to define as volatile? 回答1: From API doc of Semaphore: Memory consistency effects: Actions in a thread prior

A semaphore with beginwait function

五迷三道 提交于 2019-12-11 03:23:58
问题 I'm writing a asynchronous library using begin/end, and I need to lock objects. Currently, I'm doing this using semaphores, but calling semaphore.WaitOne() suspends the thread where it is called. I'd rather use something like BeginWait so it would return immediately and call the callback function when the semaphore is free. Is there such an object in c#? 回答1: You can use the WaitAsync method of SemaphoreSlim (.NET 4.5+) to get a Task that will be completed when the semaphore is next available

How to prevent a Perl script from running more than once in parallel

流过昼夜 提交于 2019-12-11 01:34:46
问题 I have a script that potentially runs for a long time. On Linux. While it is running, when invoked a second time by the same or a different user, it should detect that and refuse to run. I'm trying to figure out how to create a suitable semaphore that gets cleaned up even if the process dies for some reason. I came across How to prevent PHP script running more than once? which of course can be applied, but I wondering whether this can be done better in Perl. For example, Perl has the "clean

Creating semaphore with initial value of 0 make issues with execution

别来无恙 提交于 2019-12-11 00:47:59
问题 I'm learning GCD and got question about semaphore. Here is my code: class ViewController: UIViewController { var semaphore: dispatch_semaphore_t! = nil override func viewDidLoad() { super.viewDidLoad() semaphore = dispatch_semaphore_create(0) dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) { print("Entering") self.semaphoreTask() print(self.semaphore.debugDescription) } semaphoreTask() print(semaphore.debugDescription) } func semaphoreTask() { dispatch_semaphore_wait(semaphore

Semaphore_create causes kernel panic

末鹿安然 提交于 2019-12-11 00:11:04
问题 I am developing a kernel extension. I require to use wait and signal mechanism to wait for particular events (programming logics). I am trying to use semaphores as part of the kernel extension to implement the wait and signal methodology. The creation of semaphore is causing a kernel panic. Need help in figuring out the right implementation. Let me know if I am using it wrong or if there is any other simpler mechanism to wait and signal for kernel development. The current code which I am

C#线程学习笔记六:线程同步--信号量和互斥体

拈花ヽ惹草 提交于 2019-12-10 23:47:22
本笔记摘抄自: https://www.cnblogs.com/zhili/archive/2012/07/23/Mutex_And_Semaphore.html ,记录一下学习过程以备后续查用。 一、信号量(Semaphore) 信号量(Semaphore)是由内核对象维护的int变量。当信号量为0时,在信号量上等待的线程会堵塞;信号量大于0时,就解除堵塞。当在一个信号量上等待 的线程解除堵塞时,内核自动会将信号量的计数减1。在.NET下通过Semaphore类来实现信号量同步。 Semaphore类 限制可同时访问某一资源或资源池的线程数。 线程通过调用 WaitOne方法将信号量减1,并通过调用Release方法把信号量加1。 先说下构造函数: public Semaphore(int initialCount,int maximumCount);通过两个参数来设置信号的初始计数和最大计数。 下面代码演示信号量同步的使用: class Program { //共享资源 public static int number = 0; //初始信号量计数为0,最大计数为10。 public static Semaphore semaphore = new Semaphore(0, 10); static void Main(string[] args) { #region 线程同步

How to use named semaphore from child

此生再无相见时 提交于 2019-12-10 23:37:58
问题 So basically I want to suspend a bit the child process after it's creation, just so the parent prepare some data for it in a shared memory. I'm trying to use a semaphore, as suggested here: How to share semaphores between processes using shared memory. Problem nr 1 : the child can't open the semaphore. Problem nr 2 : strerror returns an int, but man strerror clearly says it returns an char *. To avoid "what have you tried": sem = sem_open("/semaphore", O_CREAT, 0644, 0); for (i = 0; i < num;