[英]Safe to access shared resource from concurrent async tasks? (C#)
我需要並行運行多個異步方法並等待所有這些方法。 (Task.WhenAll)然后在這些方法中,我需要訪問像Dictionary()這樣的共享資源。 我需要它是線程安全的嗎?
我嘗試運行以下兩個示例,兩者似乎都暗示它是多線程的(交錯消息/日期)。 http://rextester.com/AEH56431 http://rextester.com/YEB50034
這張照片有什么問題嗎? 任何人都可以確認/否認? 沒有看到C#Concurrency Master Stephen Cleary特別談到這個案例。 編輯:實際上斯蒂芬談到他的異步介紹博客中的線程模型可以提供幫助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace Rextester
{
public class Program
{
static public async Task run2()
{
Console.WriteLine("START");
await Task.WhenAll(steps(1),steps(2),steps(3),steps(4));
Console.WriteLine("STOP");
}
static public async Task steps(int i) {
await Task.Delay(100*i);
Console.WriteLine("step"+i+".1");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".2");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".3");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".4");
}
public static void Main(string[] args)
{
run2().Wait();
}
}
}
結果是:
START
step1.1
step2.1
step3.1
step4.1
step4.2
step3.2
step4.3 <-- multithreading? (2 isn't done)
step2.2
step1.2
step4.4
step3.3 <-- multithreading?
step2.3
step3.4
step1.3 <-- multithreading?
step2.4
step1.4
STOP
然后另一個變種:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
namespace Rextester
{
public class Program
{
static public async Task run()
{
Console.WriteLine("START");
await Task.WhenAll(steps(1),steps(2),steps(3),steps(4));
Console.WriteLine("\nSTOP");
}
static public async Task steps(int i) {
var a = new StringBuilder();
await Task.Delay(100*i); // This is to force to run in Async mode.
// following is a block of code with (hopefully) no "async waits". Just CPU-bound (Thread.Sleep should be blocking the thread I think?)
a.Append("\nSTART ["+Thread.CurrentThread.ManagedThreadId+"]");
a.Append("\nstep "+i+".1 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".2 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".3 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".4 >"+DateTime.Now.ToString("mm:ss.fff"));
a.Append("\nSTOP");
Console.WriteLine(a.ToString());
}
public static void Main(string[] args)
{
run().Wait();
}
}
}
其中輸出如下:
START
START [9]
step 4.1 >57:08.485
step 4.2 >57:08.891
step 4.3 >57:09.298
step 4.4 >57:09.704
STOP
START [8]
step 3.1 >57:08.391
step 3.2 >57:09.204
step 3.3 >57:10.017
step 3.4 >57:10.830
STOP
START [7]
step 2.1 >57:08.297
step 2.2 >57:09.501
step 2.3 >57:10.705
step 2.4 >57:11.908
STOP
START [6]
step 1.1 >57:08.203
step 1.2 >57:09.814
step 1.3 >57:11.424
step 1.4 >57:13.034
STOP
STOP
暗示它是多線程的(交錯消息/日期)
那些意味着它是並發的 。 而且,實際上,使用Task.WhenAll
這一行是你如何進行異步並發:
await Task.WhenAll(steps(1),steps(2),steps(3),steps(4))
請注意,每個步驟都是連續的 。 因此,每個steps
執行都將“線性地”進行,即使它是異步的。 這意味着1.1
將始終位於1.2
之前,其前面是1.3
,等等。因此,所有1.x
將按順序排列, 2.x
將按順序排列等等。但是,有多個並發執行的steps
,並且這些steps
可以交錯。
另請注意,無論是否存在多線程,都是如此。 在這種情況下(Console應用程序),沒有SynchronizationContext
或TaskScheduler
,因此每個await
都在線程池線程上恢復。 如果在單線程場景(例如UI線程)上運行相同的代碼,您將在同一線程上看到相同的順序和交錯行為。
這些“步驟”能夠並行運行。 異步函數在未完成的等待時遇到第一個等待時返回。 在這里, await Task.Delay(100*i);
。 Delay
的延續將在線程池上運行。 因此,延遲將其余代碼移動到線程池中。
如果沒有Delay
所有這些都將是連續的(包括其他睡眠)。
真的,這段代碼很巧妙。 如果你使所有阻塞異步,它會更清楚,它將同時運行。 或者,您可以插入Task.Run
調用以強制並發行為,並記錄它是有意的。
另請注意,異步IO(包括Task.Delay
)在正在進行時不會消耗工作線程。 它根本不消耗任何線程。
也許它是指導您刪除Delay
調用和單步執行代碼。 這很容易做到,因為現在所有東西都在主線程上執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.