I want to create a semaphore object that allows 100 concurrent calls and wait for the thread to be released before additional calls happen to the critical piece of code in .NET C#. I'm able to achieve this with semaphore. But since semaphore does not guarantee any order of execution with waiting threads, the further calls to that critical piece is order-less. But somehow I want to implement FIFO with waiting threads. Is there a way to do it
Asked
Active
Viewed 1,464 times
1
-
You can use a ConcurrentQueue to add a thread on task to it.Than you have a thread that would start one thread after the other to a total of 100. As soon one is finished, the managed thread would pick up the next thread and so on... By using a https://msdn.microsoft.com/en-us/library/dd267312(v=vs.110).aspx you can set the upper bounds...No Semaphore needed – Marco Feb 23 '18 at 07:57
-
Let's say there are 3 bits of time in a queue. Thread 1 gets the first one. Thread 2 gets the second. There is no guarantee that Thread 1 will start to, or finish, executing before Thread 2 does. Is that acceptable? – mjwills Feb 23 '18 at 08:02
-
2Yeah. @mjwills is right. You will need to set ids to the thread and an additional blocking code that will only allow inserting the result of the task if the id matches the expected one. – Marco Feb 23 '18 at 08:07
-
@Marco `ConcurrentQueue` wouldn't work since there is no guarantee they'll come out in the order in which you think they were added. – mjwills Feb 23 '18 at 08:31
-
We use a service bus and from there we pick up the messages. I want to do a FIFO to waiting tasks in semaphore. Is it possible? – Mar 01 '18 at 02:52
-
Please give an example of what you're trying and state what's going wrong as thoroughly as you can. – ryanwebjackson Sep 29 '18 at 16:16