Replacing task scheduler with C # on order

I was wondering if I can change a task scheduler that maps tasks to real OS threads in .NET using C #, or if I need to recompile, say, Mono runtime to do this. Thanks.

+4
source share
3 answers

System.Threading.Tasks

If you reference System.Threading.Tasks , then you need to subclass TaskScheduler , and then you can use an object of your class to initialize TaskFactory . MSDN has an example . I also found an example on the psyCodeDeveloper blog .


Threadpool

Alternatively, you can use the SynchronizationContext to process tasks sent to ThreadPool (with ThreadPool.QueueUserWorkItem , for example) being processed.

You might be interested in the Understanding SynchronizationContext series in CodeProject ( Part 1 , Part 2, and Part 3 ).


Reactive Extensions

Regarding custom schedulers in Reactive Extensions, you can also use the SynchronizationContext mentioned above, for more information check out the tutorial at introtorx.com in particular Part 4: Concurrency .


Other

Of course, you can roll back your own thread pool , although this is not recommended. In addition, you can process your threads manually - the old way.

Other task management approaches include timer scheduling and dedicated threads to do the job.


Within Theraot Libraries, you will find the Work class, which is based on a free locking queue and can be configured for any number of allocated threads, and waiting for task flows sets its time to complete tasks, any additional work is delegated by ThreadPool. This is part of the constant effort to back up System.Threading.Tasks on .NET 2.0.

In Theraot Libraries, the working class is long gone, the partial return port of System.Threading.Tasks for .NET 2.0 is available with support for the custom TaskScheduler.

Fully disclousre: As an unimaginable name is suggested, I am the author of the Threaot libraries. Sorry for the missing documentation, I am ready to help in any aspect of using libraries. Please report any bugs; there are currently no known bugs in the main thread (2013-06-26).

+12
source

I do not know about Mono, but with the Microsoft CLR you need to basically host the CLR yourself and implement the corresponding interfaces.

See this blog post for more details.

Please note that in any case this is a very relevant topic. Initially, this feature was only integrated into .NET 2.0, so Microsoft SQL Server (which has fiber mode) could map CLR threads to fibers instead of operating system threads. AFAIK, that the "experiment" is considered large, failed.

Also note that a lot of .NET (managed) code implicitly (or explicitly through P / Invoke calls in the Windows API) assumes that the mapping between the CLR threads and the OS is actually 1: 1.

+2
source

Yes, you can! You can create your own task scheduler by expanding the capabilities of the default task scheduler provided by the .NET Framework 4.0.

So, to create a custom task scheduler, you need to extend the abstract class System.Threading.Tasks.TaskScheduler and override the following methods. - QueueTask - GetScheduledTasks
- TryExecuteTaskInline

refer to this link.

0
source

Source: https://habr.com/ru/post/1488294/


All Articles