How to use hardware streams in C # network code running on a multi-core computer?

How to use hardware threads in C code running on a multi-core computer? Any example would be appreciated.

I want to run two threads in parallel on two cores of my machine. If I create regular program threads in C sharp, they can work on the same core. Is it possible to run these two threads implicitly in parallel on two cores to improve performance?

+4
source share
4 answers

You can take a look at "Processor Affinity".

You can use the System.Diagnostics namespace to make a process run on a specific processor. The next line will change the current proximity of the process to processors 1 and 2

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)3; 

The Process.ProcessorAffinity property gets or sets the processors on which you can plan to start threads in this process.

The following table shows the selection of ProcessorAffinity values ​​for a dual-processor system.

  Value Description 0 Not allowed 1 Use processor 1 2 Use processor 2 3 Use both processors 1 and 2 

For more information, go to the following MSDN link β†’ http://msdn.microsoft.com/en-us/library/system.diagnostics.process.processoraffinity.aspx

+3
source

Ah, you are using THREADS. Like in System.Threading.Thread. All known implementations of Windows.NET map them to hardware threads.

You cannot create "regular program threads" in C #. Where did you get this idea?

And the scheduler will plan all threads as it can - that is, they will work in parallel with several cores.

+3
source

If you plan to program threads, they will automatically run on any processor core that is available - for example. if you have two or four processor cores (hardware), then your computer will run your two threads in parallel (if the cores are not busy with something else at that time).

i.e. You do not need to do anything except create threads, and the computer will do everything possible to use your equipment, as well as possible.

+2
source

Usually there is no good reason for this. Windows knows more about the resources available to plan and distribute them well than in your application.

See also this CodeProject article , which shows that it is very good (along with some ways to establish affinity for the processor).

+2
source

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


All Articles