[英]Task continuation was scheduled to non thread-pool thread. Why?
在控制台应用程序中,我确实创建了自己的线程来实现工作队列。 此外,我为此唯一线程实现了自己的SynchronizationContext。
当我等待来自主线程的任务时,突然将继续(例程的其余部分)安排到了我的工作线程上,这是错误的,因为我不希望我的线程将用作随机任务的ThreadPool线程。
仅在使用Mono运行代码时,我才遇到这种现象。
这是在Mono上重现该问题的代码(在mac os x和linux系统上测试):
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main( string[] args )
{
Foo();
Console.ReadLine();
}
async static void Foo()
{
Console.WriteLine( "{0}: current thread ID={1}; scheduler={2}; context={3};",
" Main BEFORE awaiting",
Thread.CurrentThread.ManagedThreadId,
TaskScheduler.Current.Id,
SynchronizationContext.Current != null );
// MONO Output: Main BEFORE awaiting: current thread ID=1; scheduler=1; context=False;
WorkQueue queue = new WorkQueue();
// !!!
// I do expect that current context which is null will be captured for continuation.
// !!!
await queue.Enqueue();
// !!!
// As we can see our custom context was captured to continue with this part of code.
//
Console.WriteLine( "{0}: current thread ID={1}; scheduler={2}; context={3};",
" Main AFTER awaiting",
Thread.CurrentThread.ManagedThreadId,
TaskScheduler.Current.Id,
SynchronizationContext.Current != null );
// MONO Output: Main AFTER awaiting: current thread ID=4; scheduler=1; context=True;
}
}
// Custom context which does nothing but enqueues fake tasks to the queue.
//
class WorkQueueSyncContext : SynchronizationContext
{
readonly WorkQueue queue;
public WorkQueueSyncContext( WorkQueue queue )
{
this.queue = queue;
}
public override void Post( SendOrPostCallback d, object state )
{
}
public override void Send( SendOrPostCallback d, object state )
{
queue.Enqueue().Wait();
}
}
// The queue
//
class WorkQueue
{
readonly Thread thread;
class WorkQueueItem
{
public TaskCompletionSource<object> Completion
{
get;
set;
}
}
BlockingCollection<WorkQueueItem> queue = new BlockingCollection<WorkQueueItem>();
public WorkQueue()
{
thread = new Thread( new ThreadStart( Run ) );
thread.Start();
}
private void Run()
{
// Set ower own SynchronizationContext.
//
SynchronizationContext.SetSynchronizationContext( new WorkQueueSyncContext( this ) );
Console.WriteLine( "{0}: current thread ID={1}; scheduler={2}; context={3};",
" WorkQueue START",
Thread.CurrentThread.ManagedThreadId,
TaskScheduler.Current.Id,
SynchronizationContext.Current != null );
// MONO Output: current thread ID=4; scheduler=1; context=True;
// Working loop.
//
while ( true )
{
WorkQueueItem item = queue.Take();
Console.WriteLine( "{0}: current thread ID={1}; scheduler={2}; context={3};",
" WorkQueue DOING TASK",
Thread.CurrentThread.ManagedThreadId,
TaskScheduler.Current.Id,
SynchronizationContext.Current != null );
// MONO Output: current thread ID=4; scheduler=1; context=True;
// Completed the task :)
//
item.Completion.SetResult( true );
}
}
public Task<object> Enqueue()
{
TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
queue.Add( new WorkQueueItem() { Completion = completion } );
return completion.Task;
}
}
因此,这是MONO输出:
Main BEFORE awaiting: current thread ID=1; scheduler=1; context=False;
WorkQueue START: current thread ID=3; scheduler=1; context=True;
WorkQueue DOING TASK: current thread ID=3; scheduler=1; context=True;
Main AFTER awaiting: current thread ID=3; scheduler=1; context=True;
这是Windows的输出:
Main BEFORE awaiting: current thread ID=10; scheduler=1; context=False;
WorkQueue START: current thread ID=11; scheduler=1; context=True;
WorkQueue DOING TASK: current thread ID=11; scheduler=1; context=True;
Main AFTER awaiting: current thread ID=6; scheduler=1; context=False;
请注意(最后一行)上下文捕获有何不同。
编辑:
无法在Mono 3.4.0中重现,因此似乎是旧版本(至少3.2.6)中的错误;
我认为您已经在Mono运行时中发现了一个错误。 以后继续await
不应该发生与来自该被捕获的不同的同步上下文线程TaskAwaiter
,在点await
。
以下情况是可能的:
SynchronizationContext.Current == null
)。 继续行可以内联。 “可以内联”是指不需要或不能保证这样做(仍可以使用TaskScheduler.Current
或TaskScheduler.FromCurrentSynchronizationContext
进行调度以进行异步执行)。 但是,在当前Microsoft对TPL的实现下,它确实可以内联于条件#1和#2。
但是, 对于#3来说,不能内联 ,这是由常识决定的。 因此,随时向Xamarin报告错误。 首先尝试最新的Mono版本,看看问题是否仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.