[Mono-bugs] [Bug 82700][Nor] New - [PATCH] Thread.ManagedThreadId changes after a thread starts

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Tue Sep 4 22:34:04 EDT 2007


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by jlarimer at gmail.com.

http://bugzilla.ximian.com/show_bug.cgi?id=82700

--- shadow/82700	2007-09-04 22:34:04.000000000 -0400
+++ shadow/82700.tmp.6240	2007-09-04 22:34:04.000000000 -0400
@@ -0,0 +1,104 @@
+Bug#: 82700
+Product: Mono: Class Libraries
+Version: 1.2
+OS: 
+OS Details: Fedora 7
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: CORLIB
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: jlarimer at gmail.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: [PATCH] Thread.ManagedThreadId changes after a thread starts
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+
+* Description of Problem:
+
+From
+http://msdn2.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx:
+
+-Property Value-
+An integer that represents a unique identifier for this managed thread.
+
+-Remarks-
+The value of the ManagedThreadId property does not vary over time, even if
+unmanaged code that hosts the common language runtime implements the thread
+as a fiber.
+
+In Mono, the ManagedThreadId property is the OS thread ID, so is not unique
+and it changes over time (starts as 0, changes to OS thread ID after it
+starts).
+
+* Steps to reproduce the problem:
+
+---8<---8<---
+
+using System;
+using System.Threading;
+
+class ThreadTest {
+    static void Main() {
+        Thread t1 = new Thread(new ThreadStart(ThreadFunc));
+
+        int mt1 = t1.ManagedThreadId;
+        t1.Start();
+        int mt2 = t1.ManagedThreadId;
+
+        if(mt1 == mt2) {
+            Console.WriteLine("Same thread ID hasn't changed ({0})", mt1);
+        } else {
+            Console.WriteLine("Same thread ID has changed ({0}, {1})", mt1,
+mt2);
+        }
+
+        Thread t2 = new Thread(new ThreadStart(ThreadFunc));
+        t2.Start();
+        int mt3 = t2.ManagedThreadId;
+
+        if(mt2 == mt3) {
+            Console.WriteLine("Different threads have the same ID ({0})", mt2);
+        } else {
+            Console.WriteLine("Different threads had unique IDs ({0},
+{1})", mt2, mt3);
+        }
+    }
+
+    static void ThreadFunc() {
+        return;
+    }
+}
+
+---8<---8<---
+
+* Actual Results:
+
+Same thread ID has changed (0, 16034704)
+Different threads have the same ID (16034704)
+
+* Expected Results:
+
+This is the result from using the supplied patch, MS.NET gives similar
+results in .NET 1.1 and 2.0 (starting with 2 or 3 instead of 1)
+
+Same thread ID hasn't changed (1)
+Different threads had unique IDs (1, 2)
+
+* How often does this happen? 
+
+Every time
+
+* Additional Information:
+
+The attached patch gives threads a unique ID (at least it's unique until
+the Interlocked.Increment() call wraps). The patch also fixes GetHashCode
+to return the ManagedThreadId value. This is not required by the MS docs,
+but it is the observed behavior of the MS.NET 2.0 framework. I actually
+found this bug by noticing the result from GetHashCode() was changing after
+the thread started.


More information about the mono-bugs mailing list