pthreads如何进行跨线程和调度

发布于 2024-09-28 23:23:02 字数 939 浏览 1 评论 0原文

我想知道,pthreads-win32(pthreads的windows实现)如何实现跨线程?它是专门用windows API编写的吗?我检查了一些来源,似乎大多数确实是用 Windows API 编写的,但我想知道它是否也使用 Windows 调度程序在线程(和核心)之间切换,或者它是否实现自己的?具体来说,现在大多数处理器都实现了自己的调度程序(例如,我读过有关 itanium arch 的内容,硬连线逻辑支持每个核心两个线程,甚至可以使用硬件逻辑在它们之间自动切换,因此显然操作系统对多核的支持不一定是需要),因此,如果我有一个过时的操作系统,例如 Windows 32 位或其他不支持多核处理器的操作系统,那么用 pthreads-win32 编写的程序是否仍可以在多个处理器核心上运行,或者只能在一个核心上运行用过的?

pthreads 实现(未受污染的 posix 线程)怎么样?即使它们运行的​​操作系统不支持多核处理器,它们是否支持多核处理器?

我猜答案是否定的,对于 Windows 和 posix 版本,如果操作系统不支持多核,则仅使用一个核。不过,这只是一个有根据的猜测,我想确认一下,所以请发表评论。

根据侧面请求,您能否推荐一个支持多核线程执行的库,即使运行程序的操作系统不支持。如果存在的话。

另外,有没有办法确保用 pthreads 编写的两个线程在不同的内核上执行,或者操作系统(或处理器或 pthreads lib)是否自动进行分配?如果可用,pthreads 是否保证在不同的内核上执行?

干杯,瓦尔

编辑: 我知道这些问题中的大多数都是特定于实现的,所以我指的是 Windows 的 pthreads 实现 http://sourceware .org/pthreads-win32/。我之前没有特别提到它,因为据我所知,这是 Windows 上最流行且使用最广泛的 pthread 实现。

I was wondering, how does pthreads-win32 (windows implementation of pthreads) implement cross-threading? Is it written exclusively with windows API? I checked some of the sources and it seems that most is indeed written with windows API, tho i was wondering if it uses windows scheduler to switch between threads (and cores) as well or does it implement its own? Specifically, most processors these days implement their own scheduler (i've read about itanium arch for example, the hardwired logic supports two threads per core and it even automatically switches between them with hw logic, so evidently OS support for multiple cores is not necessarily needed), so if i have an obsolete OS like windows 32-bit or something, which doesn't support multi-core processors, would a program written with pthreads-win32 still run on more than one processor core or would only one core be used?

How about pthreads implementations (untainted posix threads)? Do they support multi-core processors even if the OS on which they are running doesn't?

I am guessing the answer is no, for both windows and posix versions, only one core is in use if the OS doesn't support for multiple cores. Tho this is just an educated guess and i would like to confirm it, so pls leave a comment.

On a side request, can you pls recommend a lib that DOES support for muli-core thread execution, even if the OS on which the program is running DOESN'T. If any exist ofc.

Also, is there a way to ensure two threads written with pthreads are being executed on different cores, or does the OS (or the processor, or pthreads lib) do the assignment automatically? Does pthreads guarantee execution on different cores if they are available?

Cheers, Val

EDIT:
I know most of these questions are implementation specific, so i was referring to this implementation of pthreads for windows http://sourceware.org/pthreads-win32/. I didn't specifically mention it before, because as far as i know, this is the most popular and widely used implementation of pthreads for windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-10-05 23:23:02

因此,从我得到的情况来看,在所有这一切中需要注意的最重要的事情是线程与并行性关系不大(就像具有多核处理器的 UMA)。因此,虽然线程可能是一种实现并发的技术,但它并不是确保实际并行执行的一种方法,而这正是我首先寻找的,因为我正在研究并行和分布式系统和算法。

所以一次回答一个问题。是的,pthreads 以及可能大多数(如果不是全部)其他线程 API 都是基于底层操作系统 API 的。哪个 ofc 给它们提供了与操作系统相同的限制。意思是,是的,如果操作系统(具体来说,在这种情况下,某些运行的窗口,例如 pthreads-win32)不支持多核,则始终只使用一个核心。正如 nob 提供的 wiki 页面上指出的那样:“超线程不仅要求操作系统支持多个处理器,而且还需要专门针对 HTT 进行优化,英特尔建议在使用具有以下功能的操作系统时禁用 HTT:没有如此优化。” http://en.wikipedia.org/wiki/Hyper-threading 大多数情况下的含义,仅仅硬连线处理器(基本)调度程序不足以利用多核,它必须由软件(操作系统支持)支持/使用。

虽然这可能不是决定性的证据,但我相信有足够的证据指向同一方向来证实情况确实如此。

我没有筛选 pthreads(对于 posix 兼容的操作系统)源,我猜这个 API 也是如此,因为它很可能使用底层操作系统 API。您必须自行确认这一点。 :)

另外,任何可能支持在多核上执行的潜在库,即使它们运行的​​操作系统不支持多核,您也必须自己找到它们(如果存在),请发表评论。

为了手动确保并行性(在不同内核上执行),Linux 确实提供了一种将线程固定到特定虚拟处理器(在某些条件下)的方法。要将整个进程固定到特定的(虚拟)处理器/内核,可以使用 sched_setaffinity() (来自 sched.h)。正如 nos 所指出的,pthreads 提供 pthread_setaffinity_np() 将特定线程固定到特定核心。 Windows 支持与 SetThreadAffinityMask() 类似的功能,因此显然,手动分配线程以在不同内核上并行运行是可能的(如果操作系统支持多核)。

根据我使用 pthreads 编码的经验,如果您编写使用多个线程(超过 2 个)的代码,它们应该在多个物理核心上执行(如果可用)(这可能是 pthreads 使用的操作系统功能)。

我的问题一开始就很笼统,因为大多数问题都是特定于实现的,所以很难给出一个答案。我希望这个答案足够详细,可以帮助您澄清一些事情。

干杯,瓦尔

So from what i'm getting, the most important thing to note in all of this is that threading has very little to do with parallelism (like UMA with multi-core processors). So while threading might be a technique to implement concurrency it is not a way of ensuring ACTUAL parallel execution, which is what i was looking for in the first place, since i am studying parallel and distributed systems and algorithms.

So to answer one question at a time. Yes, pthreads, and probably most (if not all) other threading APIs out there are based on the underlying OS API. Which ofc gives them the same limits that the OS has. Meaning, yes, if the OS (concretely in this case, some windows running for example pthreads-win32) doesn't support multiple cores, only one core is in use at all times. As is pointed out on the wiki page nob provided, to cite: "Hyper-threading requires not only that the operating system support multiple processors, but also that it be specifically optimised for HTT, and Intel recommends disabling HTT when using operating systems that have not been so optimized." http://en.wikipedia.org/wiki/Hyper-threading Meaning in most cases, just hardwired processors (basic) scheduler is not enough to take advantage of multiple cores, it has to be supported/used by SW (OS support).

While this might not be a definitive proof, i believe enough evidence points in the same direction to confirm this to be the case.

I did not sift through pthreads (for posix compliant OSes) sources, i am guessing the same goes for this API, since it is more than likely to use the underlying OS API. You will have to confirm this on your own. :)

Also, any potential libs out there that might support execution on multiple cores even if the OS on which they're running on doesn't support multiple cores, you will have to find them on your own (if they exist), please leave a comment.

To ensure parallelism (execution on different cores) manually, linux does provide a way to pin a thread to a specific virtual processor (under certain conditions). To pin an entire process to a specific (virtual) processor/core, sched_setaffinity() (from sched.h) can be used. As nos pointed out, pthreads provides pthread_setaffinity_np() to pin a particular thread to a specific core. Windows supports a similar functionality with SetThreadAffinityMask(), so clearly, assigning threads manually to run in parallel on different cores is possible (if the OS supports multi-cores).

From my experience coding with pthreads, if you write for code that uses multiple threads (more than 2), they SHOULD be executed on more than one physical core, if available (which is probably an OS feature used by pthreads).

My questions were quite general to begin with, since most of these things are implementation specific, it's hard to give one answer. I hope this answer is detailed enough to help you clarify a few things.

Cheers, Val

梦醒时光 2024-10-05 23:23:02

通常,每个现代操作系统本身都支持线程并将它们调度到系统的不同(虚拟)核心。操作系统提供了一些通用同步技术(如互斥体、信号量或屏障),pthread 使用这些技术来实现 pthreads API。

在某些英特尔处理器(如安腾)上,每个核心有两个线程(我认为您的意思是超线程),操作系统会看到两个“虚拟”核心。处理器确实将两个线程调度到一个物理核心上。 (参见 Wikipedia

但是,有一些示例运行时平台实现了自己的线程概念并进行调度:我认为(至少是较旧的)Java 实现有自己的调度例程。

Generally each modern OS supports Threads by itself and schedules them to the different (virtual) Cores of a System. The OS provides some general synchronization techniques (like Mutexes or Semaphores or Barriers) which are used by pthread to implement the pthreads API.

With two threads per Core (I think you mean Hyper Threading) on some Intel Processors (like Itanium) the OS sees two "virtual" Cores. The processor indeed schedules the two threads onto one physical core. (See Wikipedia)

However, there are examples where Runtime-Plattforms implement their own Thread-Conceptepts and do the scheduling: I think of (at least older) implementations of Java having their own scheduling routines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文