@@ -24,6 +24,14 @@ public sealed partial class Thread : CriticalFinalizerObject
2424 // State associated with starting new thread
2525 private sealed class StartHelper
2626 {
27+ #if TARGET_OSX
28+ // On other platforms, when the underlying native thread is created,
29+ // the thread name is set to the name of the managed thread by another thread.
30+ // However, on OS X, only the thread itself can set its name.
31+ // The thread reference here allows the native thread to set its name before the actual work starts.
32+ // See https://github.com/dotnet/runtime/issues/106464.
33+ internal Thread ? _thread ;
34+ #endif
2735 internal int _maxStackSize ;
2836 internal Delegate _start ;
2937 internal object ? _startArg ;
@@ -72,6 +80,14 @@ private void RunWorker()
7280
7381 try
7482 {
83+ #if TARGET_OSX
84+ Debug . Assert ( _thread != null ) ;
85+ if ( ! string . IsNullOrEmpty ( _thread . Name ) )
86+ {
87+ // Name the underlying native thread to match the managed thread name.
88+ _thread . ThreadNameChanged ( _thread . Name ) ;
89+ }
90+ #endif
7591 if ( start is ThreadStart threadStart )
7692 {
7793 threadStart ( ) ;
@@ -121,6 +137,9 @@ public Thread(ThreadStart start)
121137 ArgumentNullException . ThrowIfNull ( start ) ;
122138
123139 _startHelper = new StartHelper ( start ) ;
140+ #if TARGET_OSX
141+ _startHelper . _thread = this ;
142+ #endif
124143
125144 Initialize ( ) ;
126145 }
@@ -132,6 +151,9 @@ public Thread(ThreadStart start, int maxStackSize)
132151 ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
133152
134153 _startHelper = new StartHelper ( start ) { _maxStackSize = maxStackSize } ;
154+ #if TARGET_OSX
155+ _startHelper . _thread = this ;
156+ #endif
135157
136158 Initialize ( ) ;
137159 }
@@ -141,6 +163,9 @@ public Thread(ParameterizedThreadStart start)
141163 ArgumentNullException . ThrowIfNull ( start ) ;
142164
143165 _startHelper = new StartHelper ( start ) ;
166+ #if TARGET_OSX
167+ _startHelper . _thread = this ;
168+ #endif
144169
145170 Initialize ( ) ;
146171 }
@@ -152,6 +177,9 @@ public Thread(ParameterizedThreadStart start, int maxStackSize)
152177 ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
153178
154179 _startHelper = new StartHelper ( start ) { _maxStackSize = maxStackSize } ;
180+ #if TARGET_OSX
181+ _startHelper . _thread = this ;
182+ #endif
155183
156184 Initialize ( ) ;
157185 }
0 commit comments