473,396 Members | 1,891 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,396 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 1823
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...
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?
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
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
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...
0
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,...

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.