473,715 Members | 6,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hyper-Threading and ProcessorAffini ty

I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_C lassThread.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.P rocessorAffinit y 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_C lassThread.Go)) ;
mThread.Start() ;
How to do that . is there any whay to do something like :
SetProcessorAff inity( ref hthread, int mask) ?????
Nov 17 '05 #1
8 6936
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.co m

"[Yosi]" <Yo**@discussio ns.microsoft.co m> wrote in message
news:FE******** *************** ***********@mic rosoft.com...
I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_C lassThread.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.P rocessorAffinit y 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_C lassThread.Go)) ;
mThread.Start() ;
How to do that . is there any whay to do something like :
SetProcessorAff inity( ref hthread, int mask) ?????

Nov 17 '05 #2

"[Yosi]" <Yo**@discussio ns.microsoft.co m> wrote in message
news:FE******** *************** ***********@mic rosoft.com...
I create new thread as following:
Thread mThread = new Thread(new ThreadStart(m_C lassThread.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.P rocessorAffinit y 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_C lassThread.Go)) ;
mThread.Start() ;
How to do that . is there any whay to do something like :
SetProcessorAff inity( ref hthread, int mask) ?????


Here is how you can do it.

using System;
using System.Runtime. InteropServices ;

using System.Diagnost ics;
using System.Threadin g;
namespace Tester
{
class Program
{
[DllImport("kern el32")]
static extern int GetCurrentThrea dId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoW ork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(Process Thread pt in Process.GetCurr entProcess().Th reads)
{
int utid = GetCurrentThrea dId();
if (utid == pt.Id)
{
pt.ProcessorAff inity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLi ne("Set");
}
}
}
}
}
Willy.
Nov 17 '05 #3
Willy,

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

using System;
using System.Runtime. InteropServices ;

using System.Diagnost ics;
using System.Threadin g;
namespace Tester
{
class Program
{
[DllImport("kern el32")]
static extern int GetCurrentThrea dId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoW ork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(Process Thread pt in Process.GetCurr entProcess().Th reads)
{
int utid = GetCurrentThrea dId();


Does GetCurrentThrea d 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.ProcessorAff inity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLi ne("Set");
}
}
}
}
}

Willy.


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

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

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


Here is how you can do it.

using System;
using System.Runtime. InteropServices ;

using System.Diagnost ics;
using System.Threadin g;
namespace Tester
{
class Program
{
[DllImport("kern el32")]
static extern int GetCurrentThrea dId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoW ork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(Process Thread pt in Process.GetCurr entProcess().Th reads)
{
int utid = GetCurrentThrea dId();


Does GetCurrentThrea d 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.ProcessorAff inity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLi ne("Set");
}
}
}
}
}

Willy.


Kind regards,
--
Tom Tempelaere.


Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThre adId' 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
ProcessThreadCo llection, once I have the ProcessThread I can use
ProcessorAffini ty 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 (ProcessorAffin ity) 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******** ********@TK2MSF TNGP10.phx.gbl. ..

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

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

Here is how you can do it.

using System;
using System.Runtime. InteropServices ;

using System.Diagnost ics;
using System.Threadin g;
namespace Tester
{
class Program
{
[DllImport("kern el32")]
static extern int GetCurrentThrea dId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoW ork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(Process Thread pt in Process.GetCurr entProcess().Th reads)
{
int utid = GetCurrentThrea dId();


Does GetCurrentThrea d 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.ProcessorAff inity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLi ne("Set");
}
}
}
}
}

Willy.


Kind regards,
--
Tom Tempelaere.


Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThre adId' 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
ProcessThreadCo llection, once I have the ProcessThread I can use
ProcessorAffini ty 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 (ProcessorAffin ity) 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 GetCurrentThrea dId, 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 = GetCurrentThrea dId( ); // 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____AThotm ailD.Tcom/\/\@P$_0_/\/>
wrote in message news:cH******** *************@p hobos.telenet-ops.be...
Tom,

I'm not using any Logical thread Id here, both 'GetCurrentThre adId' 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
ProcessThreadCo llection, once I have the ProcessThread I can use
ProcessorAffini ty 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 (ProcessorAffin ity) 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 GetCurrentThrea dId, 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 = GetCurrentThrea dId( ); // 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****@empiric design.com> wrote in message
news:11******** ************@f1 4g2000cwb.googl egroups.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
2247
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. Problem: when I click on the hyperlink on the second page, i am getting the first page with cahed data. But i didn't declare/Use code for cache data. when i
2
1704
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 upset about the fact I had posted to the wrong area (VBE not VBA...). Anyway The problem I have is in capturing the hyperlink used to open an MS Access database. The database (Access 2000) is used to gather the results of tests then compare them...
3
2937
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 condain 4 link (<a href...>page1.aspx</a <br <a href ....>page2</aetc..). that will redirect another page ex:(page1.aspx).
5
21874
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 commands which I use to send an SMS to my email via Motorola 9505A. An error occurred after I press <Ctrl+Z> to execute the SMS. The message that I want to sent is: - "megat@geoid.biz testing from 9505A" Header of the message (pdu format) is: -
1
1689
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 trying to build a news posting system running an access DB, which will link the front page with all the news headers to a news page.
0
1310
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 specific warning?
1
4742
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 a few questions. Some getting started with Hyper-V assumptions. Please tell me if I am off base here. 1. Any recent Intel quad core processor has the needed x64 architecture with hardware assisted virtualization and data execution protection...
1
2925
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 normally. but whn i click on the hyper link of .ppt or .pps file, it shows a small windows that asks me to "open or save". can i make it open directly without this winow appearing.
8
6139
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 fields are not avaibale in act_load_structure.cfm . It says form fields are not defined. Do you know why. I appreciate your help in advance
3
1805
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> <logo></logo> ? ? ? <WEBSITE>http://www.domæne.dk</WEBSITE> ? ? ? <details>Weeknights at 8pm</details>
0
8821
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9103
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.