Channels ▼

Gaston Hillar

Dr. Dobb's Bloggers

Managed Threads Are Different From Windows Threads

March 02, 2010

.NET Framework works with managed threads and they aren't the same than Windows threads. Therefore, it isn't a good idea to use Windows API thread management functions with managed threads.When you create tasks in .NET Framework 4, managed threads are going to support the potential parallelization of their work. However, there isn't a direct relationship between the number of tasks created and the managed threads that support their execution. I talked about the relationship between tasks and managed threads in my previous post "Task Are Not Threads in .NET 4 Beta 1". Its content is still valid for .NET Framework 4 Release Candidate.

You can also assign work to managed threads through the thread pool manager, i.e., the System.Threading.ThreadPool class. Besides, it is possible to create your own managed threads by creating instances of the System.Threading.Thread class.

Developers that are used to work with Windows threads usually try to help the scheduler to assign a thread to the most appropriate hardware thread, i.e., the most convenient logical core. This way, they make sure that each thread runs in a separate hardware thread. This technique is known as "thread affinity".

Managed threads don't support methods to assign the most appropriate hardware thread to try to force the scheduler to use a specific hardware thread to execute a managed thread. .NET Compact Framework for Xbox 360 offers the SetProcessorAffinity method to the Thread class to allow developers to select the ideal hardware thread for XNA Framework games that target Xbox 360. However, this method isn't available in .NET Framework.

The problem is that a single managed thread can use one or more Windows threads to support its execution. Therefore, there isn't a one-to-one relationship between managed threads and Windows threads. In fact, a managed thread could use a Windows fiber to support its execution instead of a Windows thread according to the decisions taken by the managed scheduler.

A managed thread offers access to its ManagedThreadId property. This value also appears in the Threads Window. For example, a managed thread created by the thread pool manager, named "ThreadPool thread" has a value of 11 for its ManagedThreadId property (see Figure 1).

Figure 1: Managed threads with their ManagedThreadId values.

This ManagedThreadId value is an identifier for managed threads and it doesn't represent an Id for the Windows thread that supports its execution. Therefore, it can't be used in Windows API thread management functions. For example, it doesn't make sense to call the SetThreadAffinityMask function with a ManagedThreadId as the handle to the thread whose affinity mask is to be set (hThread). This function has the following C++ syntax definition:

DWORD_PTR WINAPI SetThreadAffinityMask(
  __in  HANDLE hThread,
  __in  DWORD_PTR dwThreadAffinityMask
);

It is possible to determine the Windows thread that is executing the code for a managed thread and to retrieve its handle. However, it still doesn't make sense to call the SetThreadAffinityMask function for this Windows thread, because the managed scheduler can continue the execution of a managed thread in another Windows thread.

If you work with tasks in .NET Framework 4 (Release Candidate) or managed threads, the managed scheduler is going to try to set the best affinity. If you believe that you really need to work with thread affinity in your algorithms, you will have to work with the Windows API or other threading libraries.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video