Example of concurrent programming with multi-threaded

So, I understand that I understand the concept of concurrent programming and multi-threading, but I was wondering if you could create multi-processor multi-threaded applications. If this makes sense, then when viewing some descriptions of multithreading, it can run on a single processor, but if you can process multiple cores using parallel, can you also multithreaded on several cores? If you can, can someone show me a simple example of this using a for loop maybe?

Multithreaded example:

 public class Test { static void Main() { Counter foo = new Counter(); ThreadStart job = new ThreadStart(foo.Count); Thread thread = new Thread(job); thread.Start(); for (int i=0; i < 5; i++) { Console.WriteLine ("Main thread: {0}", i); Thread.Sleep(1000); } } } public class Counter { public void Count() { for (int i=0; i < 10; i++) { Console.WriteLine ("Other thread: {0}", i); Thread.Sleep(500); } } } 

Parallel example:

 int n = ... Parallel.For(0, n, i => { // ... }); 
+4
source share
1 answer

Task allocation with available logical CPUs is the task of the operating system. If you are writing a multi-threaded application, the OS will control the distribution of various threads to the hardware processors in the system. You have nothing to do as a programmer to make this happen.

However, you can manage this directly - link a specific thread to work on a specific core or a subset of logical processors, but as a rule, it is less efficient than allowing the OS to control the distribution of hardware, since it has a broader understanding of all the processes that work in the system and in In most cases, it handles the distribution in the most efficient way.

In short, if your application is multithreaded, it will run on as many available cores as the system will provide.

+2
source

All Articles