semaphore

Multi-threaded BASH programming - generalized method?

雨燕双飞 提交于 2019-12-06 07:49:49
问题 Ok, I was running POV-Ray on all the demos, but POV's still single-threaded and wouldn't utilize more than one core. So, I started thinking about a solution in BASH. I wrote a general function that takes a list of commands and runs them in the designated number of sub-shells. This actually works but I don't like the way it handles accessing the next command in a thread-safe multi-process way: It takes, as an argument, a file with commands (1 per line), To get the "next" command, each process

Why is a monitor implemented in terms of semaphores this way?

♀尐吖头ヾ 提交于 2019-12-06 07:26:39
问题 I have trouble understanding the implementation of a monitor in terms of semaphores from Operating System Concepts 5.8.3 Implementing a Monitor Using Semaphores We now consider a possible implementation of the monitor mechanism using semaphores. For each monitor, a semaphore mutex (initialized to 1) is provided. A process must execute wait(mutex) before entering the monitor and must execute signal(mutex) after leaving the monitor. Since a signaling process must wait until the resumed process

Semaphore

て烟熏妆下的殇ゞ 提交于 2019-12-06 06:42:02
public class Semaphore implements java.io.Serializable { private static final long serialVersionUID = -3222578661600680210L; /** All mechanics via AbstractQueuedSynchronizer subclass */ private final Sync sync; /** * 继承AQS以实现信号量机制,使用AQS的state属性代表允许使用的信号量的数量,该Sync类有两个 * 派生类,其中一个是公平模式的,另一个是非公平模式的 */ abstract static class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 1192457210091910933L; Sync(int permits) { setState(permits); } final int getPermits() { return getState(); } //非公平模式的获取信号量 final int nonfairTryAcquireShared(int acquires) { for (;;) { int

python线程信号量semaphore(33)

强颜欢笑 提交于 2019-12-06 04:21:31
通过前面对 线程互斥锁lock / 线程事件event / 线程条件变量condition / 线程定时器timer 的讲解,相信你对线程threading模块已经有了一定的了解,同时执行多个线程的确可以提高程序的效率,但是并非线程的数量越多越好,可能对于计算机而言,你直接运行20~30线程可能没太大影响,如果同时运行上千个甚至上万个呢?我相信你电脑会直接瘫痪…… 一.semaphore信号量原理 多线程同时运行,能提高程序的运行效率,但是并非线程越多越好,而semaphore信号量可以通过内置计数器来控制同时运行线程的数量,启动线程(消耗信号量)内置计数器会自动减一,线程结束(释放信号量)内置计数器会自动加一;内置计数器为零,启动线程会阻塞,直到有本线程结束或者其他线程结束为止; 二.semaphore信号量相关函数介绍 acquire() — 消耗信号量,内置计数器减一; release() — 释放信号量,内置计数器加一; 在semaphore信号量有一个内置计数器,控制线程的数量,acquire()会消耗信号量,计数器会自动减一;release()会释放信号量,计数器会自动加一;当计数器为零时,acquire()调用被阻塞,直到release()释放信号量为止。 三.semaphore信号量使用 创建多个线程,限制同一时间最多运行5个线程,示例代码如下: # !usr

Possible Stack Corruption

会有一股神秘感。 提交于 2019-12-06 03:43:26
With reference to my previous question about GDB not pinpointing the SIGSEGV point , My thread code is as follows: void *runner(void *unused) { do { sem_wait(&x); ... if(/*condition 1 check*/) { sem_post(&x); sleep(5); sem_wait(&x); if(/*repeat condition 1 check; after atleast 5 seconds*/) { printf("LEAVING...\n"); sem_post(&x); // putting exit(0); here resolves the dilemma return(NULL); } } sem_post(&x); }while(1); } Main code: sem_t x; int main(void) { sem_init(&x,0,1); ... pthread_t thrId; pthread_create(&thrId,NULL,runner,NULL); ... pthread_join(thrId,NULL); return(0); } Edit: Having an

Using await SemaphoreSlim.WaitAsync in .NET 4

感情迁移 提交于 2019-12-06 03:24:57
问题 My application is using .NET 4. I am using await async using nuget package In my application I want to do an await on sempahore WaitAsync call as follows. SemaphoreSlim semphore = new SemaphoreSlim(100); await semphore.WaitAsync(); However I am getting following compilation error. 'System.Threading.SemaphoreSlim' does not contain a definition for 'WaitAsync' and no extension method 'WaitAsync' accepting a first argument of type 'System.Threading.SemaphoreSlim' could be found (are you missing

Windows service cannot see named semaphore

旧城冷巷雨未停 提交于 2019-12-06 00:39:16
问题 I'm trying to mediate a small windows service, making it to wait for a signal from another process during startup. For sure I know that such approach may (or even will) sometimes lead to service startup timeout. That's not the case. The problem is with named System.Thread.Sempaphore I use for mediation purposes. Semaphore is created and acquired somewhere else using following construct. There's no change GC has it as I explicitly break the execution right below given line for test purposes.

java 中的Semaphore(信号量)

情到浓时终转凉″ 提交于 2019-12-06 00:15:38
1、Semaphore可以控同时访问的线程个数 2、Semaphore类位于java.util.concurrent包下,它提供了2个构造器: //参数permits表示许可数目,即同时可以允许多少线程进行访问 public Semaphore(int permits) { sync = new NonfairSync(permits); } //这个多了一个参数fair表示是否是公平的,即等待时间越久的越先获取许可 public Semaphore(int permits, boolean fair) { sync = (fair)? new FairSync(permits) : new NonfairSync(permits); } 3、重要方法,acquire()、release()方法: acquire()用来获取一个许可,若无许可能够获得,则会一直等待,直到获得许可 release()用来释放许可。注意,在释放许可之前,必须先获获得许可 这4个方法都会被阻塞 public void acquire() throws InterruptedException { } //获取一个许可 public void acquire(int permits) throws InterruptedException { } //获取permits个许可 public void

ABAddressBookRequestAccessWithCompletion iOS 7 and semaphores

血红的双手。 提交于 2019-12-05 22:32:31
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) { // we're on iOS 6 dispatch_semaphore_t sema = dispatch_semaphore_create(0);

What is the purpose of Semaphore.wait(timeout: .now())?

☆樱花仙子☆ 提交于 2019-12-05 21:45:16
Looking at some Apple code sample, I found this: func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { // wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data. if metadataObjectsOverlayLayersDrawingSemaphore.wait(timeout: .now()) == .success { DispatchQueue.main.async { // Some processing... self.metadataObjectsOverlayLayersDrawingSemaphore.signal() } } } Context of the code : This is the delegate method when using video capture to detect a QR