I am looking to execute a bunch of ValueTask
-returning functions on a custom thread pool - i.e. on a bunch of threads I'm spawning and handling myself, rather than the default ThreadPool
.
Meaning, all synchronous bits of these functions, including any potential task continuations, should be executed on my custom thread pool.
Conceptually, something like:
class Example
{
async ValueTask DoStuff(int something)
{
// .. do some stuff in here, might complete synchronously or not, who knows ..
}
private void Test()
{
for (int i = 0; i < 1_000; i++)
{
Func<ValueTask> method = () => DoStuff(1);
MyThreadPool.Queue(method);
}
}
}
What's the best way to do this?
My current approach is something like this:
class Example
{
async ValueTask DoStuff(int something)
{
// .. do some stuff in here, might complete synchronously or not, who knows ..
}
private void Test()
{
SynchronizationContext myContext = new MyCustomThreadPoolSynchronisationContext();
TaskScheduler myScheduler;
var prevCtx = SynchronizationContext.Current;
try
{
SynchronizationContext.SetSynchronizationContext(myContext);
myScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
finally
{
SynchronizationContext.SetSynchronizationContext(prevCtx);
}
var myTaskFactory = new TaskFactory(myScheduler);
for (int i = 0; i < 1_000; i++)
{
myTaskFactory.StartNew(() => DoStuff(i).AsTask());
}
}
}
This seems to work, but having to convert the ValueTask
into a Task
and lodging it to a TaskFactory
feels exceptionally clunky.
And having to install my synchronization context, just to be able to get a hold off an appropriate TaskScheduler
(and then immediately falling back onto the old synchronization context) feels quite off too.
Are there any conceptual flaws with my current approach?
Better yet, are there any better, less awkward ways of doing this?