P
P
Peter2017-08-14 11:43:15
C++ / C#
Peter, 2017-08-14 11:43:15

A couple of questions about threads in C#. How to manage them correctly?

Good afternoon. I'm trying to figure out how to properly manage threads in C#.
Specifically interested in the following, how to distribute threads in the program?
For example, I have a conditional function like this:

void TextFunc()
{
Console.Write("Hello, World");
}
void TextFunc1()
{
Console.Write("Hello, User");
}

I'm creating a couple of threads using arrays. And I assign a function by index. How to assign a function by thread index? For example
Thread [] trd = new Thread[2];
trd[0] // тут ссылка на функцию
trd[1] // тут ссылка на функцию

Is it possible in principle?
And how in general the program is scattered on flows?
There is software that itself scales by the number of processor threads and does not lose functionality.
According to the logic that I understood, if you specify the number of threads yourself, then you yourself must sign the functions.
And if the processor has not 4 cores but 2. Does it turn out that some kind of functionality is not available? In general, explain how everything is properly organized.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FreeBa, 2017-08-14
@Morpheus_God

Everything is properly organized through Task (well, or ThreadPool if you need a lower level).
In your case, it's enough:

Task.Run(() => TextFunc());
Task.Run(() => TextFunc1());

The CLR itself will scatter everything over the CPU cores.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question