semaphore

How can I control thread count when I use “Task.WhenAll”

半腔热情 提交于 2019-12-07 07:30:31
I am verifying image urls by making an http get request asynchronously. All works fine with the code below but when I have so many Images, our firewall will block my internet access because of so many threads concurrently requesting. Therefore I was looking for a solution how to restrict the count of concurrently running threads. I ended up with this thread telling me to use SemaphoreSlim but I am somehow not able to get the idea and how to implement this? is that SemaphoreSlim wait or waitAsnyc (what is the difference anyway?) should be inside a foreach while adding tasks? Can I just create

named and unnamed posix semaphores

我只是一个虾纸丫 提交于 2019-12-07 05:26:56
问题 Planning to use a posix semaphore to sync 2 processes. Not quite sure which to use - named or unnamed. What are the advantages and disadvantages of each? How do I decide which to use? On which situations, is one preferable over the other? Thanks. 回答1: If the two processes are unrelated you should use a named semaphore. If the two process are related (i.e. forked) or if you are just using the semaphore between threads you should use unnamed. The advantages of unnamed are that you don't have to

If mutual exclusion is guaranteed, say with semaphores, is a program deadlock-free?

蹲街弑〆低调 提交于 2019-12-07 04:11:11
问题 I define mutual exclusion and deadlock as below, respectively: The mutual exclusion condition exists if at every moment, each shared resource is either assigned to exactly one process, or available. A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Say, binary semaphores are used, ensuring that only one of them can enter its critical region at the same time. Since each process does a down just before entering

strict N process synchronization using 2 semaphores

99封情书 提交于 2019-12-07 03:12:27
a few years ago I had an Operating Systems seminar. I had been tasked to create an algorithm for process synchronization using as few semaphores as possible. It should have looked like this: P1 -> P2 -> P3 -> P4 -> P5 P(n) - process Only one process running at a time and strict ordering was needed . Last year I came with solution using 3 semaphores (effectively creating a barrier). Here is my algorithm: P S1 S1 S1 S1 4W1 W0 W0 W0 W0 4S0 P S2 S2 S2 3W2 W1 W1 W1 3S1 P S1 S1 2W1 W0 W0 2S0 P S2 W2 W1 S1 P (execution is from top to bottom, each lane is a single process) P - real work which needs to

POSIX Semaphores on Mac OS X: sem_timedwait alternative

自古美人都是妖i 提交于 2019-12-06 22:24:16
问题 I am trying to port a project (from linux) that uses Semaphores to Mac OS X however some of the posix semaphores are not implemented on Mac OS X The one that I hit in this port is sem_timedwait() I don't know much about semaphores but from the man pages sem_wait() seems to be close to sem_timedwait and it is implemented From the man pages sem_timedwait() function shall lock the semaphore referenced by sem as in the sem_wait() function. However, if the semaphore cannot be locked without

Ignite.NET插件示例:分布式Semaphore(信号量)

 ̄綄美尐妖づ 提交于 2019-12-06 17:40:34
Ignite.NET从2.0版本开始,引入了 插件系统 ,插件可以仅在于.NET环境中,也可以在于.NET + Java混合环境中,本文会描述如何在后者实现插件。 为什么需要插件? Ignite.NET构建于Ignite(用Java编写)之上,JVM会在.NET进程中启动,.NET部分与Java部分进行交互,并尽可能重用现有的Ignite功能。 插件系统将此平台交互机制公开给第三方,主要场景之一是在.NET中可以使用Ignite和第三方Java API。 这种API的一个典型事例是 IgniteSemaphore ,该功能在Ignite.NET中尚不可用。 分布式Semaphore API Ignite中的Semaphore类似于 System.Threading.Semaphore ( MSDN ),但是是在整个集群中生效的,限制在所有Ignite节点上执行指定代码段的线程数。 代码大致如下: IIgnite ignite = Ignition.GetIgnite(); ISemaphore semaphore = ignite.GetOrCreateSemaphore(name: "foo", count: 3); semaphore.WaitOne(); // Enter the semaphore (may block) // Do work semaphore

VxWorks实时操作系统特点介绍

痴心易碎 提交于 2019-12-06 12:37:35
VxWorks 是美国 Wind River System 公司( 以下简称风河公司 ,即 WRS 公司)推出的一个实时操作系统。Tornado 是WRS 公司推出的一套实时操作系统开发环境,类似MicrosoftVisual C,但是提供了更丰富的调试、仿真环境和工具。 VxWorks的特点 1、VXWORKS既是一个操作系统、又是一个可以运行的最小基本程序 2、VXWORKS有BSP(可以认为是一种低层驱动),可以减小驱动程序的编写过程 3、VXWORKS具有强大的调试能力,可以在没有仿真器的情况下,通过串口调试。 4、VXWORKS具有软件DEBUG功能,可以对软件部分进行模拟调试。 5、VXWORKS具有丰富的函数库。 6、同时VXWORKS自带TCP/IP协议栈。 最大可能的减小开发者系统软硬件开发的难度,缩小开发周期,提高开发效率。 可以部分的保证软硬件开发的同步进行。 一个好的操作系统的几大特点: ● 多任务和可抢占的 ● 任务具有优先级 ● 操作系统具备支持可预测的任务同步机制 ● 支持多任务间的通信 ● 操作系统具备消除优先级转置的机制 ● 存储器优化管理 ● 操作系统的(中断延迟、任务切换、驱动程序延迟等)行为是可知的和可预测的。 ● 实时时钟服务 ● 中断管理服务 可靠性 操作系统的用户希望在一个工作稳定,可以信赖的环境中工作

How does threads sleep with interrupt disabled?

风格不统一 提交于 2019-12-06 11:50:09
问题 I am trying to understand how the code below works. This is straight out of my profs lecture slides. This P() and V() function is the part of semaphore implementation in the OS that we use in class (OS161). I think you might need understanding of the OS161 to answer my question, since its widely used, hopefully some one can answer this questions. My understanding of this code with lecture notes: X:Flow of the P() function 1. When a thread call P(), we disable interrupt 2. check if we have any

correct way to wait for dispatch_semaphore in order to wait for many async tasks to complete

我只是一个虾纸丫 提交于 2019-12-06 11:29:32
问题 I have an asynchronous method longRunningMethodOnObject:completion: this method receives an object of type 'Object' - does work with its data and then calls the completion handler. I need to call many different "longRunningMethods" and wait for all to complete. I would like all of the "longRunningMethodOnObject" to run asynchronously (parallel) to each other in the "for" loop. (I am not certain if the "longRunningMethodOnObject" runs in serial to each other but this is more of a general

CountDownLatch、Semaphore等4大并发工具类详解

好久不见. 提交于 2019-12-06 08:46:35
Java并发工具包 1.并发工具类 提供了比synchronized更加高级的各种同步结构:包括CountDownLatch、CyclicBarrier、Semaphore等,可以实现更加丰富的多线程操作。 2.并发容器 提供各种线程安全的容器:最常见的ConcurrentHashMap、有序的ConcurrentSkipListMap,实现线程安全的动态数组CopyOnWriteArrayList等。 3.并发队列 各种BlockingQueue的实现:常用的ArrayBlockingQueue、SynchorousQueue或针对特定场景的PriorityBlockingQueue。 4.Executor框架 可以创建各种不同类型的线程池,调度任务运行等,绝大部分情况下,不再需要自己从头实现线程池和任务调度器。 Java常用的并发容器 1.ConcurrentHashMap 经常使用的并发容器,JDK 1.7和1.8的底层数据结构发生了变化(后续文章会详解),这里可以建议学习顺序如下:从Java7 HashMap -> Java7 ConcurrentHashMap -> Java8 HashMap -> Java8 ConcurrentHashMap,这样可以更好的掌握这个并发容器,毕竟都是从HashMap进化而来。 2.ConcurrentSkipListMap 在乎顺序