是否可以在多个CPU内核上同时执行单线程程序? [英] Is it possible that a single-threaded program is executed simultaneously on more than one CPU core?

查看:636
本文介绍了是否可以在多个CPU内核上同时执行单线程程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行在四核Intel上编写的单线程程序时,在Windows任务管理器中可以看到,实际上我的CPU的所有四个内核或多或少都处于活动状态.一个核心要比其他三个核心更活跃,但是在这些核心上也有活动.没有其他程序(当然是OS内核旁)正在运行,对于该活动似乎是合理的.当我关闭程序时,所有活动的所有核心都下降到几乎为零.剩下的只是内核上的一点杂音",所以我很确定所有可见的活动都是直接或间接地(例如调用系统例程)来自我的程序的.

When I run a single-threaded program that i have written on my quad core Intel i can see in the Windows Task Manager that actually all four cores of my CPU are more or less active. One core is more active than the other three, but there is also activity on those. There's no other program (besided the OS kernel of course) running that would be plausible for that activitiy. And when I close my program all activity an all cores drops down to nearly zero. All is left is a little "noise" on the cores, so I'm pretty sure all the visible activity comes directly or indirectly (like invoking system routines) from my program.

操作系统或内核本身是否有可能尝试平衡所有四个内核上的某些代码或执行,即使它不是多线程程序也是如此?您是否有任何链接记录了该技术?

Is it possible that the OS or the cores themselves try to balance some code or execution on all four cores, even it's not a multithreaded program? Do you have any links that documents this technique?

该程序的一些信息:这是一个用Qt编写的控制台应用程序,任务管理器指出只有一个线程正在运行.也许Qt使用线程,但是我不使用信号或插槽,也不使用任何GUI.

Some infos to the program: It's a console app written in Qt, the Task Manager states that only one thread is running. Maybe Qt uses threads, but I don't use signals or slots, nor any GUI.

链接到任务管理器屏幕截图: http://img97.imageshack.us/img97/6403/taskmanager.png

Link to Task Manager screenshot: http://img97.imageshack.us/img97/6403/taskmanager.png

这个问题与语言无关,并且与Qt/C ++无关,我只想知道Windows还是Intel是否在所有内核上都实现了单线程代码之间的平衡.如果可以的话,这种技术如何运作?

This question is language agnostic and not tied to Qt/C++, i just want to know if Windows or Intel do to balance also single-threaded code on all cores. If they do, how does this technique work?

我能想到的是,诸如从磁盘读取等之类的内核例程是在所有内核上调度的,但这不会显着提高性能,因为代码仍必须与内核api调用同步运行.

All I can think of is, that kernel routines like reading from disk etc. is scheduled on all cores, but this won't improve performance significantly since the code still has to run synchronous to the kernel api calls.

编辑

与可怜的Windows Task Manager相比,您知道有什么工具可以对单线程和/或多线程程序进行更好的分析吗?

Do you know any tools to do a better analysis of single and/or multi-threaded programs than the poor Windows Task Manager?

推荐答案

单线程进程只能在任何给定的时间在单个内核上运行.

A single threaded process will only ever run on a single core at any given moment in time.

但是OS可能会将线程从一个内核迁移到另一个内核.虽然操作系统会尝试将线程保留在同一内核上,但是如果线程运行的最后一个内核繁忙而另一个内核空闲,则操作系统可以迁移该线程.

But it's possible for the OS to migrate a thread from one core to another. While the OS will try to keep a thread on the same core, if the last core the thread ran on is busy and another core is free, the OS can migrate the thread.

这篇关于是否可以在多个CPU内核上同时执行单线程程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆