-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Thread.UnsafeStart to avoid capturing the ExecutionContext #46594
Comments
LGTM as proposed |
Maybe |
Looks good as proposed, but we simplified it to use a default parameter. namespace System.Threading
{
public class Thread
{
void UnsafeStart(object? parameter = null);
}
} |
Just realized there's a semantic difference between Start() and Start(object), with regards to error handling around whether the Thread was created with a ThreadStart or ParameterizedThreadStart delegate. So, we should stick with the original proposal of two overloads and maintain the same semantics for the Unsafe variants. namespace System.Threading
{
public class Thread
{
public void UnsafeStart();
public void UnsafeStart(object? parameter);
}
} |
Background and Motivation
We added UnsafeStart in this PR #46181 because we needed to lazily create thread pool threads and the timer thread on the default execution context. UnsafeStart avoids capturing the current execution context and restoring it when the thread runs. There are other places where we create threads that could use similar logic:
runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEngine.Unix.cs
Lines 160 to 173 in 637e3aa
runtime/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs
Lines 202 to 207 in 637e3aa
runtime/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs
Line 299 in 637e3aa
These are places that should start on the default execution context.
Proposed API
Usage Examples
Alternative Designs
Passing a boolean to the thread constructor but that seemed like a bad idea since Start is the thing capturing the execution.
Risks
None.
The text was updated successfully, but these errors were encountered: