

System.Threading.Tasks.Task isn’t an execution unit
source link: https://ayende.com/blog/194337-B/system-threading-tasks-task-isnt-an-execution-unit?Key=885fdaff-cc07-4ab1-adad-c278fe7de264&utm_campaign=Feed%3A+AyendeRahien+%28Ayende+%40+Rahien%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

System.Threading.Tasks.Task isn’t an execution unit
From a conceptual model, a thread and a task are very similar. That is very much by design, since the Task is meant to allow you to work with asynchronous code while maintaining the illusion that you are running in a sequential manner. It is tempting to think about this in terms of the Task actually executing the work, but that isn’t actually the case.
The Task doesn’t represent the execution of whatever asynchronous process is running, the Task represent a ticket that will be punched when the asynchronous process is done. Consider the case of going to a restaurant and asking for a table, if there isn’t an available table, you cannot be seated. What the restaurant will do is hand you a pager that will buzz when the table is ready. In the same sense, a Task is just such a token. The restaurant pager doesn’t means that someone is actively clearing a table for you. It is just something that will buzz when a table is ready.
A code sample may make things clearer:
public class Restaurant { private int _occupancy; private List<(TaskCompletionSource<object>, int)> _waiters = new List<(TaskCompletionSource<object>, int)>(); public Task GetTable(int numberOfDiners) { lock (this) { if(_occupancy+ numberOfDiners < 60) { _occupancy += numberOfDiners; return Task.CompletedTask; } var tcs = new TaskCompletionSource<object>(); _waiters.Add((tcs, numberOfDiners)); _waiters.Sort((x, y) => x.Item2.CompareTo(y.Item2)); return tcs.Task; } } public void ReleaseTable(int numberOfDiners) { lock (this) { _occupancy -= numberOfDiners; while (_waiters.Count > 0) { if(_waiters[0].Item2 + _occupancy > 60) break; var tcs = _waiters[0].Item1; _waiters.RemoveAt(0); tcs.SetResult(null); } } } }
In this case, we are manually coordinating the Task using its completion source and you can see that the Task instance that was handed when trying to get a table doesn’t actually start anything. It is simply waiting to be raised when called.
That is an important aspect of how System.Threading.Tasks.Task works, because it is usually in contrast to the mental model in our head.
Recommend
-
10
My previous posts on tasks and concurrency (Threads are not the answer, Look ma, no locks and
-
7
Incremental Build Improvements, Angular 12, Distributed Task Execution, and more in Nx 12.3!Nx 12.3 includes many new features, including Incremental Build Improvements, Angular 12,...
-
6
Distributing CI: Binning and Distributed Task ExecutionAs your Nx workspaces grow, running CI on a single agent becomes unworkable. Nx’s code change analysis and computation caching allows y...
-
9
9 Task Management Software to Manage and Complete Your Tasks By Hilda Munjuri Published 19 hours ago Are you looking for task ma...
-
7
Introducing System.Threading.RateLimiting for .NET Aug 06, 2021...
-
8
An app helping you stay laser-focused on munching your tasksTask Muncher has been developed by Adam Ali of the #EverydayHustle. Stay Laser-focused on munching your weekly tasks. Prioritize what’s important with our smart, simple,...
-
14
Noway TaskRecurring tasks with Notion.Productivity
-
10
Task DashYour tasks and code reviews across platforms in one feedFree OptionsWhen working on many projects at once, issue tracking becomes fragmen...
-
11
Support is great. Feedback is even better."Thanks for checking my launch. Feel free to ask me anything. Twitter → @thekozielek Instagram → @thekozielek [email protected]"The makers of Task Finisher
-
8
Support is great. Feedback is even better."Thank you for checking the launch, please check out TaskEase on the playstore and give us feedback. Thank you!"The makers of TaskEaseSort by: ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK