<p>When launching a thread or a process in .NET or Java, is there a way to choose which processor or core it is launched on? How does the shared memory model work in such cases?</p>

[{'answer_id': 37090, 'author': 'Corey', 'author_id': 1595, 'author_profile': 'https://Stackoverflow.com/users/1595', 'pm_score': 4, 'selected': True, 'text': "<p>If you're using multiple threads, the operating system will automatically take care of using multiple cores.</p>\n"}, {'answer_id': 37096, 'author': 'Alexandru Nedelcu', 'author_id': 3280, 'author_profile': 'https://Stackoverflow.com/users/3280', 'pm_score': 1, 'selected': False, 'text': '<p>The operating system takes care of multi-threading when the virtual machine is using native threads (as opposed to green-threads), and you can\'t specify low level details, like choosing a processor for a certain thread. It is better that way because you usually have many more threads than you have processors available, so the operating system needs to do time-slicing to give all threads a chance to run.</p>\n\n<p>That being said, you can set threads priorities if you have a critical task, and a threading API usually provides this possibility. See the Java API for example: <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)" rel="nofollow noreferrer">http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)</a></p>\n\n<p>PS: there\'s something broken in the parsing engine ... I had to add the above link as plain text</p>\n'}, {'answer_id': 37117, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>is there a way to choose which processor or core it is launched on?</p>\n</blockquote>\n\n<p>You can use the task manager to tell windows what CPU(s) your program should be allowed to run on. Normally this is only useful for troubleshooting legacy programs which have broken implementations of multi-threading. To do this, </p>\n\n<ul>\n<li>Run task manager</li>\n<li>Find your process in the <code>Processes</code> window.</li>\n<li>Right click and choose <code>Set Affinity...</code></li>\n<li>Tick the checkboxes next to the CPU\'s you want to allow your application to run on. Windows will then only schedule threads from that process onto those particular CPU\'s</li>\n</ul>\n\n<p>If I recall correctly, windows will \'remember\' these settings for subsequent times your process is run, but please don\'t quote me on that - run some tests yourself :-)</p>\n\n<p>You can also do this programatically in .NET after your program has launched using using the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.processoraffinity.aspx" rel="nofollow noreferrer">System.Diagnostics.Process.ProcessorAffinity</a> property, but I don\'t think it will \'remember\' the settings, so there will always be a short period in which your app is run on whichever CPU windows sees fit. I don\'t know how to do this in java sorry.</p>\n\n<p>Note:</p>\n\n<p>This applies at the entire process level. If you set affinity for CPU0 only, and then launch 50 threads, all 50 of those threads will run on CPU0, and CPU1, 2, 3, etc will sit around doing nothing.</p>\n\n<p>Just to reiterate the point, this is primarily useful for troubleshooting broken legacy software. If your software is not broken, you really shouldn\'t mess with any of these settings, and let windows decide the best CPU(s) to run your program on, so it can take the rest of the system\'s performance into account.</p>\n\n<hr>\n\n<p>As for the \'shared memory\' model, it works the same, but there are more things that can go subtly wrong when your app is running on multiple CPU\'s as opposed to just timeslices on a single one.</p>\n\n<p>For an eye-opening example, read <a href="http://ridiculousfish.com/blog/archives/2007/02/17/barrier/" rel="nofollow noreferrer">this ridiculousfish article about CPU\'s and Memory Barriers</a>.</p>\n\n<p>It\'s aimed at OSX development on PowerPC, but general enough that it should apply everywhere. IMHO it\'s one of the top ten \'all developers should read this\' articles I\'ve read.</p>\n'}, {'answer_id': 37127, 'author': 'sieben', 'author_id': 1147, 'author_profile': 'https://Stackoverflow.com/users/1147', 'pm_score': 0, 'selected': False, 'text': '<p>I have used this in a couple of programs because my core 0 was kind of messed up.</p>\n\n<pre><code>// Programmatically set process affinity\nvar process = System.Diagnostics.Process.GetCurrentProcess();\n\n// Set Core 0\nprocess.ProcessorAffinity = new IntPtr(0x0001);\n</code></pre>\n\n<p>or</p>\n\n<pre><code>// Set Core 1\nprocess.ProcessorAffinity = new IntPtr(0x0002);\n</code></pre>\n\n<p>More on this in "<a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.processoraffinity.aspx" rel="nofollow noreferrer">Process.ProcessorAffinity Property</a>".</p>\n'}, {'answer_id': 37998, 'author': 'Mark Harris', 'author_id': 4026, 'author_profile': 'https://Stackoverflow.com/users/4026', 'pm_score': 0, 'selected': False, 'text': '<p>I would have a look at the Parallel extensions to the .NET framework. It is still in <a href="http://en.wikipedia.org/wiki/Software_release_life_cycle#Open_and_closed_beta" rel="nofollow noreferrer">CTP</a>, however it supposed to make the best use of multi core processors. The easiest place to get started for .NET is on the <a href="http://blogs.msdn.com/pfxteam/" rel="nofollow noreferrer">parallel teams blog</a>.</p>\n\n<p>As for Java I have no idea.</p>\n'}]
['https://Stackoverflow.com/questions/37089', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/646/']