There is such a code with two for loops:

for (int a = 0; a < numb1; a++) { //тут весь код для запуска и работы потоков для цикла ниже. for (int d = 0; d < numb2; d++)//нужно чтобы этот подцикл выполнялся в 10 потоках (не суть количество потоков может быть разное) паралельно. { //тут не важно (код для выполнения в потоках). } } 

It is necessary that the second inner loop be executed in separate parallel threads, and preferably all the code for starting threads would fit inside the first loop, such as can be implemented in general in C #?

  • and what Thread didn’t please you? - Monomax
  • the fact is that this whole code is already executed in a multithreading and I need the FOR loop of the second to be executed in separate in parallel threads ... - GeneratorSveta
  • Can. What you want is called parallelization. Read "c # how to parallelize loop", or how to parallelize a specific task. Here, for example, regfordev.blogspot.com/2011/04/blog-post.html - nick_n_a

2 answers 2

everything is simple

 for (int a = 0; a < numb1; a++) { Parallel.For(0, numb2, d => { //тут не важно (код для выполнения в потоках). }); } 

Indicating the number of threads

 for (int a = 0; a < numb1; a++) { Parallel.For( 0, numb2, new ParallelOptions() {MaxDegreeOfParallelism = 42}, d => { //тут не важно (код для выполнения в потоках). }); } 
  • Parallel.For will not go to regret, it is necessary that the number of streams can be specified exactly ... - GeneratorSveta
  • @GeneratorSveta updated the answer - tym32167
  • Ok ATP right now, I’ll try, why I’ll try because it seems like Parallel.For he puts the streams himself, depending on the processor cores, as I remember, although I may be confusing ... - GeneratorSveta
  • @GeneratorSveta puts itself if you don’t tell it clearly how many threads you need - tym32167

There are several solutions (as always in programming: D)
You can, as mentioned earlier, parallelize this loop with your hands using Thread / ThreadPool / Tasks , etc.
But most likely you want to use something like the Parallel class, which already has a For method that parallelizes it for you: D
His call will look something like this for you:

 Parallel.For(0, numb2, (x)=>{ /* а тут 'неважный' код */}) 
  • Parallel.For will not go to regret, it is necessary that the number of streams can be specified exactly ... - GeneratorSveta
  • @GeneratorSveta, the idea of ​​what is planned to do is most likely not true, but this is not for me to judge) The most cost - effective implementation by code is through ThreadPool: D - Kamushek
  • I thought it was possible to start a pure cycle so without using a trapdoor, according to ThreadPool it is possible to use a variant based on the same code, how exactly do you propose to implement this with a ThreadPool? - GeneratorSveta
  • one
    @generatorSveta, just put in the thread pool a limit on the number of threads ( threadPool.SetMaxThreads ) that you need. For the code inside the chicle, it is better to create a method (and not to use a lambda expression, because in this case they can spoil the data due to capture) and just leave this loop, and in it call QueueUserWorkItem with our method. - Kamushek
  • ok ATP right now I will try with the pool and by the answer above with the lambda expression, how can you use the continue and break operators? atoms cause an error: "It is impossible to transfer control from the body of an anonymous method or lambda expression" these two operators in the code ... - GeneratorSveta