semaphore

ubuntu: sem_timedwait not waking (C)

佐手、 提交于 2019-12-20 15:25:44
问题 I have 3 processes which need to be synchronized. Process one does something then wakes process two and sleeps, which does something then wakes process three and sleeps, which does something and wakes process one and sleeps. The whole loop is timed to run around 25hz (caused by an external sync into process one before it triggers process two in my "real" application). I use sem_post to trigger (wake) each process, and sem_timedwait() to wait for the trigger. This all works successfully for

Does SemaphoreSlim (.NET) prevent same thread from entering block?

自古美人都是妖i 提交于 2019-12-20 12:04:13
问题 I have read the docs for SemaphoreSlim SemaphoreSlim MSDN which indicates that the SemaphoreSlim will limit a section of code to be run by only 1 thread at a time if you configure it as: SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); However, it doesn't indicate if it stops the same thread from accessing that code. This comes up with async and await. If one uses await in a method, control leaves that method and returns when whatever task or thread has completed. In my example, I use

What is the consensus number for semaphores?

一笑奈何 提交于 2019-12-20 09:57:34
问题 (I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)? 回答1: The consensus number for a mutex would be 1. It's trivially clear that a mutex will be wait-free for a single thread. From its definition, it's also clear that a mutex is no longer wait-free for two threads. The consensus number therefore is >=1 and <2, so it must be 1. Likewise, other

What is the Mutex and semaphore In c#? where we need to implement? [closed]

拟墨画扇 提交于 2019-12-20 09:53:38
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . What is the Mutex and semaphore in C#? Where we need to implement? How can we work with them in multithreading? 回答1: You should start at MSDN. System.Threading.Mutex: A synchronization primitive that can also be used for interprocess synchronization. System.Threading.Semaphore:

Proper way to try-catch a semaphore

谁说我不能喝 提交于 2019-12-20 01:59:06
问题 What is the proper way to wrap semaphore actions in a try-catch block? What happens if the acquire action is interrupted after it has acquired some number, but not all, of the permits requested? How do you know how many to release again? Should the release go in a "finally" block, but then aren't you possibly releasing permits you didn't get if the action was interrupted? try { lock.acquire(permits); //Do some things that require synchronization //Make sure to release all of the permits again

(笔记)Linux内核学习(七)之内核同步机制和实现方式

好久不见. 提交于 2019-12-19 22:43:10
一 原子操作 指令以原子的方式执行——执行过程不被打断。 1 原子整数操作 原子操作函数接收的操作数类型——atomic_t //定义 atomic_t v;//初始化 atomic_t u = ATOMIC_INIT(0); //操作 atomic_set(&v,4); // v = 4 atomic_add(2,&v); // v = v + 2 = 6 atomic_inc(&v); // v = v + 1 = 7 //实现原子操作函数实现 static inline void atomic_add(int i, atomic_t *v) { unsigned long tmp; int result; __asm__ __volatile__("@ atomic_add\n"     "1: ldrex %0, [%3]\n"     " add %0, %0, %4\n"     " strex %1, %0, [%3]\n"     " teq %1, #0\n"     " bne 1b"   : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)   : "r" (&v->counter), "Ir" (i)   : "cc"); } 2 原子位操作 //定义 unsigned long word = 0; //操作

iOS开发之用到的几种锁整理

*爱你&永不变心* 提交于 2019-12-19 16:36:09
1. iOS中的互斥锁 在编程中,引入对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为“互斥锁”的标记,这个标记用来保证在任一时刻,只能有一个线程访问对象。 1.1 @synchronized (self) - (void)lock1 { @synchronized (self) { // 加锁操作 }} 1.2 NSLock - (void)lock2 { NSLock *xwlock = [[NSLock alloc] init]; XWLogBlock logBlock = ^ (NSArray *array) { [xwlock lock]; for (id obj in array) { NSLog(@"%@",obj); } [xwlock unlock]; }; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSArray *array = @[@1,@2,@3]; logBlock(array); });} 1.3 pthread pthread除了创建互斥锁,还可以创建递归锁、读写锁、once等锁 __block pthread_mutex_t mutex; pthread_mutex_init(&mutex, NULL);

fork(), problems with multiple children

亡梦爱人 提交于 2019-12-19 11:56:56
问题 I edited a little bit : for ( ii = 0; ii < nbEnfants; ++ii) { switch (fork()){ case -1 : { printf("\n\nSoucis avec fork() !!! \n\n"); exit(0); }; case 0 : { EEcrireMp(ii); }break; default : { tabPidEnfants[ii] = p; usleep(50000); ELireMp(nbSect, nbEnfants,tabPidEnfants); }; } } My problem : i get to many child, like a bomb of children spawning. How can i stop those child ? the break should stop it no ? Thanks 回答1: So, when you fork a process, the new process is an identical copy of the parent

Waiting on multiple semaphores without busy-waiting (C/C++ Linux)

百般思念 提交于 2019-12-19 03:18:11
问题 If I have more than one semaphore, how can I have a process block until at least one of the semaphores is free? I know I can do this with a busy-wait loop such as: // blocks until one of the semaphores in sems is free, returns // index of semaphore that was available int multiple_sem_wait(sem_t **sems, int num_sems) { while (true) { for (int i = 0; i < num_sems; ++i) { if (sem_trywait(sems[i]) == 0) { return i; } } } } But is there a way to do this without a busy-loop? Perhaps there's some

Semaphore

核能气质少年 提交于 2019-12-19 01:45:17
package com.company.bingfa; import java.util.concurrent.Semaphore; class WC extends Thread{ private Semaphore sem; WC(Semaphore sem){ this.sem = sem; } @Override public void run() { if (sem.availablePermits()>0) System.out.println(getName()+"发现有空位"); else System.out.println(getName()+"没有位子,等待中.."); try { sem.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName()+"开始使用..."); try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(getName()+"使用完毕---"); sem.release(); } } public class MySemaphore { public static