Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 Gholam222.1K
Rate this: bad
good
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.
 
[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
  Permalink  
v3
Comments
Mehdi Gholam at 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 at 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
losmac at 20-Jun-12 15:34pm
   
Nice explained, +5!
Sergey Alexandrovich Kryukov at 20-Jun-12 15:41pm
   
Thank you, Maciej. --SA
Mehdi Gholam at 21-Jun-12 4:27am
   
It seems to work, I have added the code as a solution.
Rate this: bad
good
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;
            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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 367
1 Abhinav S 226
2 thatraja 220
3 OriginalGriff 195
4 Peter Leow 160
0 Sergey Alexandrovich Kryukov 8,849
1 OriginalGriff 5,035
2 Peter Leow 3,949
3 Maciej Los 3,535
4 Abhinav S 3,148


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 21 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid