Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Within a Task execution does the Thread.CurrentThread.ManagedThreadId stay the same, given that the Task are executed within a Threadpool and are recycled?
Posted

The following seems to work ok :
C#
public static void Main(string[] args)
{
    for (int i = 0; i < 1000000; i++)
        Task.Factory.StartNew(() => work());
}

private static void work()
{
    int i = Thread.CurrentThread.ManagedThreadId;
    dosomework(i,0);
}

private static void dosomework(int i, int c)
{
    if (c == 3) return;
    Thread.Sleep(1);
    int j = Thread.CurrentThread.ManagedThreadId;
    if (i != j)
        throw new Exception("failed");
    dosomework(i,++c);
}

So as far as this test goes it seems that the call chain is executed on a single Thread.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jun-12 11:04am    
Thank you for sharing this information. Unfortunately, but as I said before, it does not prove anything -- nothing guarantees that this thread would always be the same.

May I know what would be the purpose or application of this assumption?

--SA
Mehdi Gholam 21-Jun-12 11:09am    
I'm trying to implement transactions for RaptorDB with this (keep track of the threads/tasks work done).
To best of my knowledge, System.Threading.Tasks.Task is implemented by automatic mapping on threads, and we know that on Windows .NET threads are implemented on the base of OS threads (not fibers or some .NET specific mechanism). That said, the system should not guarantee that every time a task is executing its time slice, the current thread being executed is the same in all cases.

You see, you can easily try to confirm it by experiment, but the problem is: if you observe that a current thread is sometimes different, it would confirm it, but if you observe that the current thread is the same for the same task, it would not prove anything.

[EDIT]

Indirectly, what I say is confirmed by Microsoft documentation: http://www.codeproject.com/script/Answers/Post.aspx?aid=407532[^]:
Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms (like hill-climbing) that determine and adjust to the number of threads that maximizes throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. To complement this, widely-known work-stealing algorithms are employed to provide load-balancing.

From all this documentation article, I don't see a reason why a task instance should be guaranteed to run by the same thread. I think it would be safest to say: this is implementation-dependent.

[END EDIT]

If you go so far to try it out, of find out exact documentation explicitly explaining this matter, please notify me by commenting on this answer.

—SA
 
Share this answer
 
v3
Comments
Mehdi Gholam 20-Jun-12 14:30pm    
The real question is if the ID stays the same down the task call chain (and not multiplexed by the runtime to different thread id's). Maybe I will have to do some testing for this!

Thanks Sergey!
Sergey Alexandrovich Kryukov 20-Jun-12 14:34pm    
You are very welcome.

I don't think you can rely on the assumption that the thread ID is guaranteed to stay the same. What would be your technique based on such assumption. As to the testing, I hope you can observe that the thread changes, not stay the same. Because -- if you won't observe it, your test proves nothing, do you understand that?

--SA
Maciej Los 20-Jun-12 15:34pm    
Nice explained, +5!
Sergey Alexandrovich Kryukov 20-Jun-12 15:41pm    
Thank you, Maciej.
--SA
Mehdi Gholam 21-Jun-12 4:27am    
It seems to work, I have added the code as a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900