Bug 325367 (MONO82700) - [PATCH] Thread.ManagedThreadId changes after a thread starts
Summary: [PATCH] Thread.ManagedThreadId changes after a thread starts
Status: RESOLVED FIXED
Alias: MONO82700
Product: Mono: Class Libraries
Classification: Mono
Component: CORLIB (show other bugs)
Version: 1.2
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 325566
  Show dependency treegraph
 
Reported: 2007-09-05 05:34 UTC by Jon Larimer
Modified: 2007-09-25 15:42 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
Proposed patch (1.95 KB, patch)
2007-09-05 05:34 UTC, Thomas Wiest
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 20:52:06 UTC


---- Reported by jlarimer@gmail.com 2007-09-04 22:34:04 MST ----

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.



---- Additional Comments From jlarimer@gmail.com 2007-09-04 22:34:36 MST ----

Created an attachment (id=172614)
Proposed patch




---- Additional Comments From gert.driesen@pandora.be 2007-09-05 14:32:54 MST ----

Added (NotWorking) unit test to ThreadTest.cs.

Imported an attachment (id=172614)

Unknown bug field "cf_op_sys_details" encountered while moving bug
   <cf_op_sys_details>Fedora 7</cf_op_sys_details>
Unknown operating system unknown. Setting to default OS "Other".

Comment 1 Dick Porter 2007-09-25 15:42:59 UTC
Patch applied to svn head, r86337.  Thanks.