Click here to Skip to main content
13,141,576 members (52,536 online)
Rate this:
Please Sign up or sign in to 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 20-Jun-12 6:55am
Mehdi Gholam343.7K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


Indirectly, what I say is confirmed by Microsoft documentation:[^]:
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.


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.

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?

losmac 20-Jun-12 15:34pm
Nice explained, +5!
Sergey Alexandrovich Kryukov 20-Jun-12 15:41pm
Thank you, Maciej.
Mehdi Gholam 21-Jun-12 4:27am
It seems to work, I have added the code as a solution.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The following seems to work ok :
        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;
        private static void dosomework(int i, int c)
            if (c == 3) return;
            int j = Thread.CurrentThread.ManagedThreadId;
            if (i != j)
                throw new Exception("failed");

So as far as this test goes it seems that the call chain is executed on a single Thread.
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?

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).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 21 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100