473,394 Members | 1,658 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.

How to *force* threads to run on certain processors?

Hi There!

We have 2 processor server (Not dual core, actual seperate processors).

Is it possible from .NET to force some threads to run on certain processor
(say on processor #2)? For instance, run UI stuffs on processor #1 and run
other background stuffs on processor #2.

Possible?

Thanks in advance,
Jon
May 6 '07 #1
2 1935
Jon B wrote:
Hi There!

We have 2 processor server (Not dual core, actual seperate
processors).
Is it possible from .NET to force some threads to run on certain
processor (say on processor #2)? For instance, run UI stuffs on
processor #1 and run other background stuffs on processor #2.

Possible?
From Win32, you can do this using SetThreadAffinityMask.

The CLR processing model does not guarantee that a .NET thread corresponds
to a Win32 thread, but in practice they do, so you could probably P/Invoke
to SetThreadAffinityMask and get something that "works", but that could be
broken by the CLR in some future release when/if .NET threads are no longer
1:1 associated with Win32 threads.

If you're hosting your code in SQL Server, this may already be the case (I
think that feature was pulled before 2005 shipped, but I don't know for
sure).

Generally, messing with thread affinity is a bad idea - it rarely improves
the performance of your application and can do it serious harm.

-cd
May 7 '07 #2
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O5****************@TK2MSFTNGP02.phx.gbl...
Jon B wrote:
>Hi There!

We have 2 processor server (Not dual core, actual seperate
processors).
Is it possible from .NET to force some threads to run on certain
processor (say on processor #2)? For instance, run UI stuffs on
processor #1 and run other background stuffs on processor #2.

Possible?

From Win32, you can do this using SetThreadAffinityMask.

The CLR processing model does not guarantee that a .NET thread corresponds
to a Win32 thread, but in practice they do, so you could probably P/Invoke
to SetThreadAffinityMask and get something that "works", but that could be
broken by the CLR in some future release when/if .NET threads are no
longer 1:1 associated with Win32 threads.

If you're hosting your code in SQL Server, this may already be the case (I
think that feature was pulled before 2005 shipped, but I don't know for
sure).

Generally, messing with thread affinity is a bad idea - it rarely improves
the performance of your application and can do it serious harm.

-cd


Note that you don't need to use PInvoke for this, you can assign a thread to
a specific processor by means of the ProcessorAffinity property of the
System.Diagnostics.ProcessThread class.
But as you said, this is a bad idea, the OS will try to keep the UI threads
on the processor they were initially created on anyway

Willy.

May 7 '07 #3

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

Similar topics

6
by: Thomas Womack | last post by:
If I have a dual-processor hyperthreaded machine (so with four CPU contexts), will a python program distribute threads over all four logical processors? I ask because I'm fairly sure that this...
29
by: Jeffrey Maitland | last post by:
Hello all, I am in the process of writing a multithreading program and what I was wondering is a sleep command in an executing function will affect the threads below it? Here is a basic example...
2
by: nagaraj_hayyal | last post by:
Hi All, thanks for reading this post. just wanted to know about the ratio of threads and processors. i am working on c++ on AIX5.2 platform. my c++ program are multithreaded programs. ...
3
by: nrhayyal | last post by:
Hi All, thanks for reading this post. just wanted to know about the ratio of threads and processors. i am working on c++ on AIX5.2 platform. my c++ program are multithreaded programs. In a...
16
by: LP | last post by:
Hi, Considering code below. Will it make GC to actually collect. One application creates new instances of a class from 3rd party assembly in a loop (it has to). That class doesn't have .Dispose or...
4
by: kaiteriteri | last post by:
I have a time-consuming VB.net application that i'd like to thread over 2 processors (that's all i've got in my machine!) and, hopefully, get it done in half the time. On running, the application...
2
by: Jon B | last post by:
Hi There! We have 2 processor server (Not dual core, actual seperate processors). Is it possible from .NET to force some threads to run on certain processor (say on processor #2)? For...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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.