473,394 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Hyper-Threading and ProcessorAffinity

I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
In my system I have 2 Process devices ,(CPU0 and CPU1) , I want to create 3
Threads, 2 of those thread to run in CPU0 and the last one in CPU1 , I get
some advice to use the ProcessThread.ProcessorAffinity which seems OK to me .
The problem is how and where to use it ?
Should I change the :
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
mThread.Start();
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????
Nov 17 '05 #1
8 6912
Yosi,

You could try and do it, however, this isn't a good idea in .NET. In
..NET, the Thread class doesn't represent a physical thread. It is a logical
thread of execution. Because of this, you don't know how the current thread
you are running on maps to the physical OS thread that it is running on.

If you really need to do this, I would perform your tasks in unmanaged
code and set the affinity there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
In my system I have 2 Process devices ,(CPU0 and CPU1) , I want to create
3
Threads, 2 of those thread to run in CPU0 and the last one in CPU1 , I
get
some advice to use the ProcessThread.ProcessorAffinity which seems OK to
me .
The problem is how and where to use it ?
Should I change the :
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
mThread.Start();
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????

Nov 17 '05 #2

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
In my system I have 2 Process devices ,(CPU0 and CPU1) , I want to create
3
Threads, 2 of those thread to run in CPU0 and the last one in CPU1 , I
get
some advice to use the ProcessThread.ProcessorAffinity which seems OK to
me .
The problem is how and where to use it ?
Should I change the :
Thread mThread = new Thread(new ThreadStart(m_ClassThread.Go));
mThread.Start();
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????


Here is how you can do it.

using System;
using System.Runtime.InteropServices;

using System.Diagnostics;
using System.Threading;
namespace Tester
{
class Program
{
[DllImport("kernel32")]
static extern int GetCurrentThreadId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoWork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
int utid = GetCurrentThreadId();
if (utid == pt.Id)
{
pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLine("Set");
}
}
}
}
}
Willy.
Nov 17 '05 #3
Willy,

"Willy Denoyette [MVP]" <wi*************@telenet.be> schreef in bericht
news:uR*************@TK2MSFTNGP15.phx.gbl...
[...]
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????
Here is how you can do it.

using System;
using System.Runtime.InteropServices;

using System.Diagnostics;
using System.Threading;
namespace Tester
{
class Program
{
[DllImport("kernel32")]
static extern int GetCurrentThreadId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoWork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
int utid = GetCurrentThreadId();


Does GetCurrentThread return the same ID each time in this loop? I heard
that .NET threads are logical threads, not physical threads. So technically,
wouldn't it be possible that another physical thread starts executing the
logical thread?
if (utid == pt.Id)
{
pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLine("Set");
}
}
}
}
}

Willy.


Kind regards,
--
Tom Tempelaere.
Nov 17 '05 #4

"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
wrote in message news:VC*********************@phobos.telenet-ops.be...
Willy,

"Willy Denoyette [MVP]" <wi*************@telenet.be> schreef in bericht
news:uR*************@TK2MSFTNGP15.phx.gbl...
[...]
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????


Here is how you can do it.

using System;
using System.Runtime.InteropServices;

using System.Diagnostics;
using System.Threading;
namespace Tester
{
class Program
{
[DllImport("kernel32")]
static extern int GetCurrentThreadId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoWork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
int utid = GetCurrentThreadId();


Does GetCurrentThread return the same ID each time in this loop? I heard
that .NET threads are logical threads, not physical threads. So
technically, wouldn't it be possible that another physical thread starts
executing the logical thread?
if (utid == pt.Id)
{
pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLine("Set");
}
}
}
}
}

Willy.


Kind regards,
--
Tom Tempelaere.


Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThreadId' and
'pt.Id ' return the OS thread id. I'm using the first to get the current OS
thread ID and use the ID to search the corresponding thread in the
ProcessThreadCollection, once I have the ProcessThread I can use
ProcessorAffinity to set the affinity mask. As long as the thread runs it
keeps the same Id (and the same logical thread which I don't care).
I ran this on an 8 way box, funny how you can disturb the system by playing
with this :-)). IMO this API (ProcessorAffinity) shouldnt be used from
managed code, honestly I don't see why it was included in the framework,
while other more important features are still missing, cheep to implement I
guess.

Willy.
Nov 17 '05 #5
Hi Willy,

"Willy Denoyette [MVP]" <wi*************@telenet.be> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...

"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
wrote in message news:VC*********************@phobos.telenet-ops.be...
Willy,

"Willy Denoyette [MVP]" <wi*************@telenet.be> schreef in bericht
news:uR*************@TK2MSFTNGP15.phx.gbl...
[...]
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????

Here is how you can do it.

using System;
using System.Runtime.InteropServices;

using System.Diagnostics;
using System.Threading;
namespace Tester
{
class Program
{
[DllImport("kernel32")]
static extern int GetCurrentThreadId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoWork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
int utid = GetCurrentThreadId();


Does GetCurrentThread return the same ID each time in this loop? I heard
that .NET threads are logical threads, not physical threads. So
technically, wouldn't it be possible that another physical thread starts
executing the logical thread?
if (utid == pt.Id)
{
pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLine("Set");
}
}
}
}
}

Willy.


Kind regards,
--
Tom Tempelaere.


Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThreadId' and
'pt.Id ' return the OS thread id. I'm using the first to get the current
OS thread ID and use the ID to search the corresponding thread in the
ProcessThreadCollection, once I have the ProcessThread I can use
ProcessorAffinity to set the affinity mask. As long as the thread runs it
keeps the same Id (and the same logical thread which I don't care).
I ran this on an 8 way box, funny how you can disturb the system by
playing with this :-)). IMO this API (ProcessorAffinity) shouldnt be used
from managed code, honestly I don't see why it was included in the
framework, while other more important features are still missing, cheep to
implement I guess.

Willy.


What I meant is that .NET has logical threads, the OS has physical threads
(if I read that correctly). Logical threads are executed on physical
threads, but I don't know if they need to execute on the same physical
thread each time the logical thread's time-slice phases in.

Actually I don't know the real details about how .NET framework implements
threading on WinOS. But if the logical thread can be executed by different
physical threads, then if the logical thread phases out right after the call
to GetCurrentThreadId, the logical thread could be rescheduled on a
different physical thread so the ID you got is no longer the correct one.
This is just my train of thought now, considering the difference between
physical and logical threads.

A sketch of my thoughts

// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_X]
int utId = GetCurrentThreadId( ); // utId == P_X
// --> the logical thread ends its time slice
....
// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_Y]
if (utId == pt.Id) // utId != P_Y

I'm not saying that this is possible, as I said I know too little of the
internal details. I think that the logical and the physical threads are
tightly coupled in the implementation of MS's .NET framework, but should
they be? Why did they make the distinction between logical and physical
threads otherwise? Does the documentation say that a logical thread is
always scheduled on the same physical thread?

Kind regards,
--
Tom Tempelaere.
Nov 18 '05 #6
Tom, see inline...

Willy.

"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
wrote in message news:cH*********************@phobos.telenet-ops.be...
Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThreadId' and
'pt.Id ' return the OS thread id. I'm using the first to get the current
OS thread ID and use the ID to search the corresponding thread in the
ProcessThreadCollection, once I have the ProcessThread I can use
ProcessorAffinity to set the affinity mask. As long as the thread runs it
keeps the same Id (and the same logical thread which I don't care).
I ran this on an 8 way box, funny how you can disturb the system by
playing with this :-)). IMO this API (ProcessorAffinity) shouldnt be used
from managed code, honestly I don't see why it was included in the
framework, while other more important features are still missing, cheep
to implement I guess.

Willy.
What I meant is that .NET has logical threads, the OS has physical threads
(if I read that correctly). Logical threads are executed on physical
threads, but I don't know if they need to execute on the same physical
thread each time the logical thread's time-slice phases in.


Logical thread don't execute, only physical threads do, and each logical
thread (an object instance) is associated with a physical thread. This
associating is fixed and exists for the lifetime of the logical thread.
Also logical threads do not get scheduled, so they aren't "time-sliced".
Actually I don't know the real details about how .NET framework implements
threading on WinOS. But if the logical thread can be executed by different
physical threads, then if the logical thread phases out right after the
call to GetCurrentThreadId, the logical thread could be rescheduled on a
different physical thread so the ID you got is no longer the correct one.
This is just my train of thought now, considering the difference between
physical and logical threads.

No, as said before a logical thread is associated with a physical thread
which ceases to exist when the thread procedure exists. Note, I'm not
talking about Threadpool threads, only regular self started threads.
A sketch of my thoughts

// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_X]
int utId = GetCurrentThreadId( ); // utId == P_X
// --> the logical thread ends its time slice
...
// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_Y]
if (utId == pt.Id) // utId != P_Y

I'm not saying that this is possible, as I said I know too little of the
internal details. I think that the logical and the physical threads are
tightly coupled in the implementation of MS's .NET framework, but should
they be? Why did they make the distinction between logical and physical
threads otherwise? Does the documentation say that a logical thread is
always scheduled on the same physical thread?


Again, a logical thread doesn't get scheduled, it's a CLR data structure not
an active OS object, the CLR keeps track of the asociations and the OS
threads keeps a reference (in TLS) to the corresponding CLR thread's data
structure.
The distinction is simply an abstraction, at the application level you
shouldn't care about the underlying implementation, that way the CLI
implementor has the freedom to implement threading as he sees fit (depending
on the available OS platform services). For instance the CLI (or an Hosting
process like SQL2005) could associate a Fiber instead of a OS thread to a
logical thread.
In such a case, multiple logical threads would run on the same OS thread,
and these logical threads would end on the same CPU if you set the OS thread
affinity.

Willy.
Nov 18 '05 #7
I can think of a couple of reasons you might/may want to set processor
affinity (even from managed code).

1. You're selling an application that you license by the processor.
They've only bought a one-processor license, so that's all they get.

2. You are USING an application that you license by the processor (SQL
Server) and you're not using all the processors for it. You might want
to have your application running on the non-SQL Server processors so
that it doesn't slow down the SQL Server ones. Memory would be a
separate issue, of course.

Chuck

Dec 16 '05 #8

<ch****@empiricdesign.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.com...
I can think of a couple of reasons you might/may want to set processor
affinity (even from managed code).

1. You're selling an application that you license by the processor.
They've only bought a one-processor license, so that's all they get.

HT cores are not physical CPU's! You ain't gonna license your application
per logical CPU do you?
2. You are USING an application that you license by the processor (SQL
Server) and you're not using all the processors for it. You might want
to have your application running on the non-SQL Server processors so
that it doesn't slow down the SQL Server ones. Memory would be a
separate issue, of course.

Same here SQL server license is not per logical CPU, an HT P4 for instance
is considered one single CPU for SQL server (and for the Windows OS).

Willy.
Dec 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: VIJAY KUMAR | last post by:
Hi pals, I am using 2 web forms (pages). In first page, i have Datagrid control and on second page i have a hyper link control to the first page and Add value to the data grid/Database. ...
2
by: AlanHill1965 | last post by:
Hi All, I have developing a database system for the last couple of weeks and got stuck on one part of the functionality. I have posted this question over at MSDN, but no joy, perhaps they got...
3
by: ajaspersonal | last post by:
"i want to change font_style (hyper link<a href...>text</a>) normal to italics.when load a page" this is my problem but that label included in one 'USERCONTROL' This user controls may...
5
by: McGuard | last post by:
I am trying to send an SMS using hyper terminal in windows XP pro. I am connecting my Satellite Phone (Motorola Iridium 9505A) to my notebook via serial cable (RS232) . Below are the sample...
1
by: simons | last post by:
Hi All, I'm trying to add a hyper link to one of the filed in my DataList control. I added a link button but could not bound it to an ID filed and then link it to the page that i need. I'm...
0
by: mclewell | last post by:
i created an html hyper link field and clicking on the data in the field produces a security warning pop up requiring a yes click before the link is activated, is there a way to turn off this...
1
by: =?Utf-8?B?Q29kZVNsaW5nZXI=?= | last post by:
I plan to build my own 2008 Server/Hyper-V system and will not be using one of the tested Dell or HP systems from the release notes and could use some pointers as to my assumnptions and answers to...
1
by: Kalaram | last post by:
I am makeing a CD, and in this CD i have alot of .pdf & .ppt & .pps files and i have one html page that i use as index to all the files, when i click on the hyper link of .pdf file , it opens...
8
by: jolakammu | last post by:
Guys, I am calling onclick="document.form.submit();" in a hyper clink. On click, it goes to a fuseaction where act_load_structure.cfm loads the structure with form variable. But the form...
3
Fary4u
by: Fary4u | last post by:
Hi is any body know how to create the hyper link with following code i can't do it ? <?xml version="1.0" encoding="utf-8" standalone="yes"?> <images> <pic> <image>img/1.jpg</image>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.