I was wondering if I can change the task scheduler that maps tasks to the real OS threads in .NET using C#, or if I need to recompile, say, the Mono runtime to do this. Thanks.
-
Sure you can write your own.... what are you using? a) Thread Pool b) System.Threading.Tasks b) Reactive Extensions, c) other? I think you are using System.Threading.Tasks if so, what you want is to subclass System.Threading.Tasks.TaskScheduler. – Theraot Jun 26 '13 at 12:58
-
Are you talking about tasks as in System.Threading.Tasks.Task, or a more general concept? You can absolutely write your own TaskScheduler. – Jon Skeet Jun 26 '13 at 12:59
-
You can host .NET from your own (non-managed) application - much as SQL Server does - and take complete control over such things - is that what you're after? – Damien_The_Unbeliever Jun 26 '13 at 12:59
3 Answers
System.Threading.Tasks
If you refer to System.Threading.Tasks then what you need is to subclass TaskScheduler and then you can use an object of your class to initialize a TaskFactory. There is an example in MSDN. I have also found an example in the blog psyCodeDeveloper.
ThreadPool
Aside from that you could use SynchronizationContext to handle the way the tasks posted to the ThreadPool (with ThreadPool.QueueUserWorkItem for example) get handled.
For that you may be interested in the series Understanding SynchronizationContext at CodeProject (part 1, part 2 and part 3).
Reactive Extensions
As for the custom schedulers in Reactive Extensions, you can also use the SynchronizationContext mentioned above, for more information check the tutorial at introtorx.com in particular Part 4: Concurrency.
Others
Of course you can roll your own thread pool, although doing that is not advised. Aside from that you could handle your threads manually - the old way.
Other approaches to handle tasks include scheduling with timers and having dedicated threads to do the work.
As part of the Theraot Libraries you will find the class Work which is based on a lock free queue and can be configured to have any number of dedicated threads, also threads waiting on tasks contribute their time to execute tasks, any extra work is delegated to the ThreadPool. This is part of an ongoing effort to backport System.Threading.Tasks to .NET 2.0.
In Theraot Libraries the Work class has been gone for a while now, a partial back port of System.Threading.Tasks
for .NET 2.0 is available with support for custom TaskScheduler.
Full disclousre: As the unimaginative name suggest, I'm the author of the Threaot libraries. Sorry by the missing documentation, I willing to help in any aspect of using the libraries. Please report any bugs, I have currently (2013-06-26) no known bugs in the master branch.
I don't know about Mono, but with the Microsoft CLR you need to basically host the CLR yourself and implement the relevant interfaces.
See this blog entry for more information.
Note that anyway that this is a very involved topic. The feature was initially only integrated in .NET 2.0 so that Microsoft SQL Server (which has a fiber-mode) could map CLR threads to fibers instead to operating system threads. AFAIK that "experiment" is considered larged failed however.
Also note that a lot of .NET (managed) code implicitly (or explicitly via P/Invoke calls to Windows API) assumes that the mapping between CLR and OS threads is actually 1:1.

- 3,185
- 1
- 25
- 34

- 47,778
- 10
- 99
- 143
Yes you can! You can build your own custom task scheduler by extending the features of the default task scheduler provided by .Net framework 4.0.
So, to build a custom task scheduler, you would need to extend the System.Threading.Tasks.TaskScheduler abstract class and override the following methods.
- QueueTask
- GetScheduledTasks
- TryExecuteTaskInline
refer to this link.

- 710
- 1
- 8
- 18
-
Do not post link only answers. Especially if the link is behind a paywall. – Aron Aug 24 '22 at 04:52