Chryso

Hello,

I developed a multi threaded application. The main thread takes huge resources while the other threads are lighter.

Now I want to make it work like this on a multicore/processor computer
The main thread => second processor
All other thread => first processor

I am using System.Threading to create and launch the threads. However through a Thread reference I can only access the managedThreadID and not the real ID which is found in Process.GetCurrentProcess().Threads[] . I also cannot use processorAffinity either since System.Threading.Thread does not have this attribute.

So I have to list the thread in my process and then affect the affinity. However ID's in the list of threads in the process are not the same as the managed ID.

Am I doing it totally wrong Does a function exists which can identify an ID from the managed thread pool with the thread ID of the process thread list

I do have a solution to know which threads has which ID, by listing all the threads in the process list, saving the ID list, launch a thread, find the ID which differed and that would by the ID of my thread.

I was just wondering if something simplier existed and if I did things kinda right or not

Regards,
Chryso


Re: Visual C# General Threading ID issues

Peter Ritchie

You should just let the system manage what processor is used for what thread. The system takes all other threads into consideration when it schedules. If a thread in another process at a higher priority is busy using a particular CPU, why would you want to force your thread on to that CPU only to be starved when it could get at least a portion of another CPU (depending on what's using the other CPU)

The short story is you can't access managed threads as system threads; they don't have a one-to-one relationship in the current CLR and in future CLRs they may not even be system threads (they may be fibers).

The best you can hope for is to use BeginThreadAffinity and EndThreadAffinity that ask the host to keep the current thread on the current processor (don't switch to another processor). But, again, you're circumventing the systems thread scheduler and will likely have slower performance rather than faster.






Re: Visual C# General Threading ID issues

HWM

I agree, let Windows worry about scheduling it really does know what its doing.






Re: Visual C# General Threading ID issues

Chryso

Hello,
Well it would seem sensible to put a thread on a processor when you know that the processor will not have anything else on it.
Well I guess I ll let windows and the xbox handle it Smile
Regards,
Chryso




Re: Visual C# General Threading ID issues

Peter Ritchie

How will you know that there are no other threads running on that processor Your application may have two threads, and the hardware may have two processors; but there's dozens of other threads in other processes that need the processor too.




Re: Visual C# General Threading ID issues

Chryso

Hello,

I did not want to disable any other processus running on a processor. I just want that, for my application, I have an heavy thread on one processor. And hundreds of small threads launched and run on the second processor.

It was really regarding my application. It is the thread of my application I wanted to separate on different processors.

Regards,
Chryso

PS: Actually, I have 4 "Main" Threads, and I have an artificial intelligence. For each element controlled by an artificial intelligence I have a thread.
So I wanted to have my main threads running on a processor each (or hardware thread) and all the small AI on other processors.
Since some of the main threads process buffers, making them run without interruption from context switches to a small AI thread seems sensible.
(Yes, I do not have only 2 processors Smile )




Re: Visual C# General Threading ID issues

Peter Ritchie

I understand that it's just for you application; but it's not just your application running. Why would you want to force your threads onto 4 processors if one processor is currently in use by another application resulting in one of your threads starved, when you can let the system divvy up as much of the 3 other processors that your 4 threads can handle When the 4th processor comes available, .NET will use that too, switching the active thread to the least-used processor when it can.




Re: Visual C# General Threading ID issues

Chryso

I had forgotten about the starving issue Sad

But I wanted to do it because I d rather have 100 light thread on a specific processor where lots of context switch will happen because they are light thread and are sleeping most of the time (I specifically call the Sleep method, releasing the left quantum time). Than having 50 small with 1 big... but since they are mostly sleeping it might be more pertinent to put huge with bits of small ones so it can use their share of quantums... mmm I wonder, darn

Now as I understand it, context switch costs, the more you have the more it costs. Having hundreds of threads that force quantum switch means lots of context switch => processor heavy

If my main threads which are doing some heavy stuff are less interrupted by context switch, am I not better off

Darn I do not remember how quantums of time are allotted in a comp...

Oh, and on an XBox, you have 2 hardware threads for OS that are reserved, meaning I am pretty much sure nothing is running on the other 4.

But I guess I ll just leave it to the OS and hope it will be able to optimize this a bit.

Thanks for the answer.

Regards,
Chryso