dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.65k forks source link

How to let coroutine on a single thread #58050

Open CeSun opened 3 years ago

CeSun commented 3 years ago

I am a C++ programmer, mainly developing stateful services. In C++, I use the network to drive asynchronous callback functions. The program is single-threaded. But the callback is really annoyed. So I want to use c# to do this work and replace the callback function with await/async.

The default c# coroutine will take idle threads from the thread pool to run, but I want to run the coroutines on a single thread to ensure the safety of critical resources.

I did the following:

https://github.com/CeSun/Server/blob/master/Frame/SingleThreadSynchronizationContext.cs

void Main(string[] args)
{
   var ctx = new SingleThreadSynchronizationContext();
   SynchronizationContext.SetCurrentContext(ctx);
   ctx.RunNew(() => {
      _ = MainSync();
   });
   while(true)
   {
       ctx.Run();
    }
}

Task MainSync()
{

}

The Run function only takes 10 to 20 functions to execute in order to guarantee the number of cycles per second. I will do some update operations before the loop starts. For example, the timer may need to be updated here.

But the final performance is not high, I want to know if it is written incorrectly and does not meet the specifications. The server QPS is only 60...

ghost commented 3 years ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

Issue Details
I am a C++ programmer, mainly developing stateful services. In C++, I use the network to drive asynchronous callback functions. The program is single-threaded. But the callback is really annoyed. So I want to use c# to do this work and replace the callback function with await/async. The default c# coroutine will take idle threads from the thread pool to run, but I want to run the coroutines on a single thread to ensure the safety of critical resources. I did the following: https://github.com/CeSun/Server/blob/master/Frame/SingleThreadSynchronizationContext.cs ``` void Main(string[] args) { var ctx = new SingleThreadSynchronizationContext(); SynchronizationContext.SetCurrentContext(ctx); ctx.RunNew(() => { _ = MainSync(); }); while(true) { ctx.Run(); } } Task MainSync() { } ``` The Run function only takes 10 to 20 functions to execute in order to guarantee the number of cycles per second. I will do some update operations before the loop starts. For example, the timer may need to be updated here. But the final performance is not high, I want to know if it is written incorrectly and does not meet the specifications. The server QPS is only 60...
Author: CeSun
Assignees: -
Labels: `area-System.Threading`, `untriaged`
Milestone: -
CeSun commented 2 years ago

Is there any progress?

huoyaoyuan commented 1 year ago

but I want to run the coroutines on a single thread to ensure the safety of critical resources.

The server QPS is only 60...

I believe this is actually the reason. Cross-thread scheduling is costful.