Search code examples
c#multithreadingparallel.foreach

How to know thread ID for each thread spawn by Parallel.Foreach


I have a scenario something like this:

int arr[100];

Parallel.Foreach(arr, (a) => { some processing});

Now, this code will spawn 100 child threads. How can I know thread ID for each child thread in "some processing" logic?


Solution

  • You could use

    Thread.CurrentThread.ManagedThreadId 
    

    but note that your parallel foreach is not forced to actually create 100 threads.