Is there a way to find the thread id inside Parallel.FoEach loop. I tried using var threadId = Thread.CurrentThread.ManagedThreadId - 1;
, but it didn't give me the correct index I was looking for.
Here is an simple example:
private void TestProgram()
{
int numThreads = 1;
var values = new List<float>();
for (int i = 0; i < numThreads; ++i)
{
values.Add(i);
}
var data = new List<int>();
for (int i = 0; i < numThreads; ++i)
{
data.Add(i);
}
Parallel.ForEach(data, new ParallelOptions{MaxDegreeOfParallelism = numThreads}, i =>
//foreach (var i in data)
{
var threadId = Thread.CurrentThread.ManagedThreadId - 1; // make the index to start from 0
values[threadId] += i;
});
}
Even after setting the MaxDegreeOfParallelism to 1
, I still get the threadId
to be greater than 1.
Is there a way to find the thread id inside Parallel.ForEach in above scenario?
Note: I could have used Parallel.For in the example I used. But my question is to find it inside Parallel.ForEach
Parallel. ForEach is like the foreach loop in C#, except the foreach loop runs on a single thread and processing take place sequentially, while the Parallel. ForEach loop runs on multiple threads and the processing takes place in a parallel manner.
Parallel. ForEach uses managed thread pool to schedule parallel actions. The number of threads is set by ThreadPool.
The execution of Parallel. Foreach is faster than normal ForEach.
ForEach loop works like a Parallel. For loop. The loop partitions the source collection and schedules the work on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs.
Since Parallel.ForEach is part of the Task Library, Task.CurrentId will get you closer to what you are looking for:
var data = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Parallel.ForEach(data, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
{
Console.WriteLine(Task.CurrentId);
});
output is 1 1 1 1 1 1 1 1 1 1 2 2 1
However, there is a disclaimer in the docs:
Task IDs are assigned on-demand and do not necessarily represent the order in which task instances are created. Note that although collisions are very rare, task identifiers are not guaranteed to be unique.
ThreadIDs are assigned by the underlying environment, and have no guarantee of being from 0 to [num of threads] or even being consistent from run to run.
There's only a few contracts with respect to threadIDs, and even these aren't guaranteed:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With