1

Possible Duplicate:
How do I spawn threads on different CPU cores?

I am using C#. I want to write the program using the threading concept. I just want to know how to run the threads in different processors. Here I attach my partial code:

Thread t1 = new Thread(threadJobA);
Thread t2 = new Thread(threadJobB);
t1.Start();
t2.Start();
Community
  • 1
  • 1
vps
  • 1,337
  • 7
  • 23
  • 41
  • 3
    http://stackoverflow.com/questions/32343/how-do-i-spawn-threads-on-different-cpu-cores – Habib Oct 11 '12 at 04:57
  • 2
    The question @Habib points out is good. Simply put: Don't worry about it. It is up to the OS to determine which threads run on which CPU. The more you try to "help" it run the way you think it should, the worse off it will end up running. – Jonathon Reinhart Oct 11 '12 at 05:03
  • @Habib Is there is any via to find out which thread is running on which processor? – vps Oct 11 '12 at 05:07
  • @vps Look over there ----------> In the **Related** list, you'll see [How do I find the processor on which my thread is running in C#?](http://stackoverflow.com/questions/2340727/how-do-i-find-the-processor-on-which-my-thread-is-running-in-c) – Jonathon Reinhart Oct 11 '12 at 05:57

3 Answers3

5

The short answer, like I said in my comment is: "Don't worry about it." If you have to ask this question, it is definitely not worth any amount of effort you'd put forward.

That said, the way to really do this is to set the "Thread Affinity Mask" which controls which logical processor the thread is allowed to run on. I'm not sure you can even do this with CLR threads (it may blow up and the world will end.) But if you have a plain ol' thread, created using CreateThread, then you can set the affinity mask using SetThreadAffinityMask.

But really. Don't do this.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

Why you want to take the call on processors? What is the rational behind that?

Its purely OS responsibility to delegate jobs/tasks to processors. This is to manage execution time of all running process/tasks. If you have a quad-core processor, still its not necessary that all processors will be always given same load. OS is managing the PCB to share the available resources among the running process.

TPL is good to execute your code parallely, wise apply of TPL will improve the performance.

vinodpthmn
  • 1,062
  • 14
  • 28
-1

You could simply use the Task Parallel Library and that will do a great job at delegating the threads to CPU Cores.

When you want to run threads in different processors you'll come up against crossing "AppDomain" boundaries.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    That may depends on the end goal though. TPL is a great abstraction layer, but it often helps to understand what it is you are abstracting over. – Roman Oct 11 '12 at 05:13
  • 1
    -1 This is not true at all. **The OS** determines which threads run on which CPU. And, which CPU core is executing which thread has nothing to do with AppDomains. See [What is AppDomain?](http://stackoverflow.com/questions/574708/what-is-appdomain) – Jonathon Reinhart Oct 11 '12 at 05:20
  • The only thing TPL does in this regard is it "...scales the degree of concurrency dynamically to most efficiently use all the processors that are available." This prevents there being too many more threads than cores, which will eventually *hurt* rather than *help* you. – Jonathon Reinhart Oct 11 '12 at 05:23
  • This is why [so] is cool, you learn stuff from each other! – Jeremy Thompson Nov 28 '12 at 11:31