简体   繁体   中英

Replacing the task scheduler in C# with a custom-built one

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM