1

I searched the web and the answers I found were tentative. That is, people thought this is what happens but were not sure.

Assuming the proper precautions were taken to synchronize and prevent collisions, can threads from the same process run at the same time on multicore systems?

The reason I'm asking is that I'm design a language to do autothreading. That is, having the computer decide when and where threading is to take place. But if all threads from one process can only be time-sliced and not run simultaneously, then autothreading will not create an improvement in speed.

3
  • 1
    If they couldn't be, then no process could be multi-threaded. See this SO question and answers: What is the difference between a process and a thread Sep 12, 2014 at 19:49
  • 1
    Process can be multithreaded if the threads were time sliced within their process time slice. Which they would have to be on a single processor computer. Again, your answer is not definitive. Sep 12, 2014 at 21:08
  • From a hardware implementation point of view I can see how a person can ask this question. The hardware could be simpler if a process' memory space was restricted to a specific core, thus threads would be restricted as well. Multi-threaded would imply logical execution isolation but not actual parallel processing. Only separate processes could actually be concurrent. HOWEVER, this is not the case, it is parallel. You can check by creating 4 threads each with a lengthy loop :)
    – Michael
    Feb 8, 2018 at 0:27

3 Answers 3

2

Yes, they can. In most operating system I'm aware of, and certainly under Windows and Linux, there is nothing that says that only one of a process's threads can run at a time. In fact the Windows scheduler pays very little attention to "which process did a thread come from?" in making its decisions. On a system with n cores, n threads can run at literally the same time. It doesn't matter if they're all from one process, from four different processes, or any combination.

For Windows you can demonstrate this easily with the "cpustres" app. You can find this in the "Windows Internals book tools", a package distributed by Mark Russinovich at the sysinternals tools site. Get your system as quiet as possible, then use cpustres to create two threads and set their "activity level" to maximum. Then check your CPU time graphs in Task Manager. Or use Process Explorer to watch the CPU time of the two threads.

The Windows Internals book includes complete information on how the scheduler (not the "task scheduler", the thread scheduler) does its job. It's a long read, but worth it. The latest version of that book covers Windows 7 but there haven't been any large changes in this area since then.

1

Yes, the whole point of multiple CPUs or CPU cores is that all of them can do different things at the same time; otherwise, why would anyone make a multi-core system if all other cores had to wait for the first one to finish its slice?

It's also why synchronization primitives like spinlocks, mutexes, barriers &c. exist and are necessary in the first place, which is proven by the hundreds of bugs found in real software (kernels and regular multi-threaded programs) that were caused by multiple threads accessing the same data structures at once after forgetting to "lock" them.

1
  • 4
    Yes, modern OSes can run many processes at the same time. That does not mean they can run multiple threads from the same process at the same time. And those bugs you write of, will happen if the processes were just time sliced. They need not be run simultaneously to cause those bugs. Sep 12, 2014 at 20:45
0

I was also looking for this answer, in specific for Intel processors. In the case of Intel, the Hyper-threading technology overview makes this very clear:

Intel® Hyper-Threading Technology (Intel® HT Technology) uses processor resources more efficiently, enabling multiple threads to run on each core

Therefore, yes! The threads from a single process will be distributed among the many cores and will run in parallel.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .