im deep diving in threading for my custom thread pool implementation.
My plan is the following when connecting big servers:
Im crafting a tcp/udp/named/anonymous pipe library.
1 thread running on each core waiting for input (keyword thread affinity) 1 socket per thread(abstracted as a stream to support named pipes)
1 thread running on each core sending data (keyword thread affinity) 1 socket per thread(abstracted as a stream to support named pipes)
Result on 4 core server should be: (Abstracted as a interface ISequentialDataSource, ISequentialDataSink)
Core 0 => SendLoop, ReceiveLoop, ReceiveDeserializationLoop Core 1 => SendLoop, ReceiveLoop, ReceiveDeserializationLoop Core 2 => SendLoop, ReceiveLoop, ReceiveDeserializationLoop Core 3 => SendLoop, ReceiveLoop, ReceiveDeserializationLoop My theory is that i have the best data transfer rate when doing it like that.
https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/threading.md
Outlines that a c# thread does NOT map to a real thread. :'(
That strikes my plan :p
The native methods to support this behavior are there:
windows: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadaffinitymask (I also read about ProcessingThread. It supports assigning the affinity but these threads do not map to the Threading.Threads. Those ProcessingThreads fetched can be fetched and configured using Process.Threads.) linux: https://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html android: (ignored, this is for servers. maybe there is also pthreads for android... will investigate) Is it possible to tie a managed thread to a specific core? Or is there any way to find the ProcessThread of a Threading.Thread? (And is it then guranteed to only handle the one thread or what will i tie to a processor then...)
This will be open source(MIT license): https://github.com/christopher-vonblum/RadFramework.Libraries
mfg, Christopher
Ps. Related and unsolved:
How to start a thread on a specific core?
How can I set processor affinity to a thread or a Task in .NET?
How to set the processor affinity of a managed thread?
Set thread processor affinity in Microsoft .Net
Help Microsoft :p
Threadobject is a managed construct unrelated to threads, but as soon as you start the execution it is mapped to a real thread. The CLR could implement this differently but to my knowledge it has always been mapped 1:1 to OS threads. You can inspect the relationship between threads and thread objects in WinDbg.asyncawaiteverywhere, then you don't need to worry about threads at all. The thread pool and the Windows kernel will manage the tasks and assign them to the correct threads and processor cores. This method will give you the highest performance, as it means you don't need to create and destroy threads for every request