Can multithreading be disabled? #6
-
I want to respond to requests only from the main thread. Use case being a game server, which is typically not multithreaded supported. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Yes, you can set your callback to be syncronized. Static methods are synchronized to the type, and instance methods are synchronized to the instance they are contained in. Only one thread executes with the method marked Synchronized. Example: using Sisk.Core.Http;
using Sisk.Core.Routing;
using System.Runtime.CompilerServices;
class Program
{
static void Main(string[] args)
{
HttpServer server = HttpServer.Emit(5155,
out HttpServerConfiguration config,
out var host,
out var router);
router += new Route(RouteMethod.Get, "/", Index);
server.Start();
Console.WriteLine(server.ListeningPrefixes[0]);
Thread.Sleep(-1);
}
[MethodImpl(MethodImplOptions.Synchronized)]
static HttpResponse Index(HttpRequest request)
{
Thread.Sleep(1000);
return new HttpResponse(200).WithContent("Hello, world!");
}
} There, |
Beta Was this translation helpful? Give feedback.
-
Great, thank you! |
Beta Was this translation helpful? Give feedback.
-
Is there a way to instead manually trigger when it handles requests, to fully control when it executes and which thread it runs on? For context, I am wanting to handle requests to have it tied to the game server loop, so it runs as part of its update cycle |
Beta Was this translation helpful? Give feedback.
Yes, you can set your callback to be syncronized. Static methods are synchronized to the type, and instance methods are synchronized to the instance they are contained in. Only one thread executes with the method marked Synchronized.
Example: