c# - Task.WhenAll() only executes 2 threads at a time? -


In this problem I am trying to cache a single value, let's call it foo. If the price is not cashed, then it takes during recovery.

My problem is not that apply it , but test .

To test it, I close the work together using Task 5. When receiving all () cached values, the first person enters the lock and receives the asynchronous value, while the other 4 threads should wait on the lock. After waiting, they should check again the cached value once again, it shows that it has been recovered from the first thread, and it has been returned without the second recovery.

  [TestClass] public class class 2 {private readonly semaphore semaphore = new semaphore (1, 1); Private Bull? Foo; Private async Tasks & lt; Bool & gt; GetFoo () {bool fooValue; // Nuclear operation to obtain current Fu Balls? CurrentFoo = this.foo; If (currentFoo.HasValue) {Console.WriteLine ("Foo already recovered"); FooValue = currentFoo.Value; } And {Sikfire. WaitOne (); {// Atomic operation to get current foo currentFoo = this.foo; If (currentFoo.HasValue) {// Waiting for Qunk Console / Foo was recovered. WrightLine ("Recover Fu while Waiting"); FooValue = currentFoo.Value; } Else {// to simulate foo value to get console. Wrightite ("get new foo"); Awaiting work Delle (timespace.formsconds (5)); This.foo = true; FooValue = True; }} Semaphore.Release (); } Return fooValue; } [Testimation] Public ASINCC Task Test () [Task [] getFooTasks = new [] {this.GetFoo (), it.getfu (), it.getfu (), this.getfu (), this. Getfu (),}; Awaiting work When all (getFooTasks); }}   

In my actual testing and production code, I am retrieving the value through an interface and joking about the interface that is using Mike. At the end of the examination I verify that the interface is called only 1 time (pass), instead of & gt; Foo already received Foo already recovered Foo already recovered

Output:

  P> Although you can see from the output of the test that it is not in my expectation, it looks like  only 2 threads are executed simultaneously  while others Threads were waiting until the first two were completed by the GetFoo () method  Enter .  

Why is this happening? Is it because I am running it inside the VS unit test? Note that my test still passes, but the way I expect it, I suspect there are some restrictions on the number of threads in the VS unit test.

function. When all () does not work start < / Em> - it just waits for them .

Similarly, a async method actually does not compromise equality - it does not present a new thread, or anything like that. You can only get new threads if:

  • You are waiting for something that has not been completed, and your synchronization reference schedule to continue on a new thread (which WinForms will not be used in context, for example, it will only reuse UI threads)
  • You explicitly use . Work. , and the task scheduler creates a new thread to run it. (It definitely does not need to be done.)
  • You obviously start a new thread.

    Honestly, Blocking Semaphore Methods in an async method I find very wrong You really do not accept the idea of ​​anisinization Are doing ... I have not tried to analyze your code at all , but I think that you read more about this, async works, And how to best utilize it.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -