Hans
Hans

Reputation: 2832

.NET threading model

When we create a thread in .NET via ThreadPool or Task.Run or a Thread object, is that thread mapped to a single Windows thread or it could be mapped to multiple ones? In other words, what's the Threading Model of .Net CLR? Can somebody please shed a light here?

PS: Similar question about JVM has been asked here, but I couldn't find similar one for .NET.

Upvotes: 1

Views: 174

Answers (1)

Sunius
Sunius

Reputation: 2907

By default, each managed thread is mapped to a single OS thread. However, by using CLR hosting APIs, you can migrate managed threads to different native OS threads.

From MSDN:

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.

However, you can ask CLR to not move your managed thread to another OS thread by calling Thread.BeginThreadAffinity.

This means .NET threading model is N:M.

Upvotes: 2

Related Questions