我如何产卵在不同的CPU内核线程? [英] How do I spawn threads on different CPU cores?

查看:235
本文介绍了我如何产卵在不同的CPU内核线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我在C#中的程序做了一些计算昂贵,如编码的WAV文件的列表,为MP3的。通常我会恩code中的文件之一的时间,但让我们说,我想计划要弄清楚有多少CPU内核我已经和旋转起来编码线程上的每个核心。所以,当我运行在一个四核CPU的程序,该程序计算出它是一个四核CPU,数字出有四个内核一起工作,然后派生四线程的编码,其中的每一个在其自己单独的CPU上运行。我将如何做到这一点?

Let's say I had a program in C# that did something computationally expensive, like encoding a list of WAV files into MP3's. Ordinarily I would encode the files one at a time, but let's say I wanted the program to figure out how many CPU cores I had and spin up an encoding thread on each core. So, when I run the program on a quad core CPU, the program figures out it's a quad core CPU, figures out there's four cores to work with, then spawns four threads for the encoding, each of which is running on its own separate CPU. How would I do this?

和这会不会有什么不同,如果核心呈S $ P $垫放到多个物理CPU的?如,如果我有一台机器有两个四核CPU的就可以了,有没有什么特殊的考虑或者是在两个模具的八个核心在Windows视为相等?

And would this be any different if the cores were spread out across multiple physical CPU's? As in, if I had a machine with two quad core CPU's on it, are there any special considerations or are the eight cores across the two dies considered equal in Windows?

推荐答案

不要刻意这样做。

而不是使用线程池。线程池是,你可以为一个新的线程查询框架的机制(实际上是一类)。

Instead use the Thread Pool. The thread pool is a mechanism (actually a class) of the framework that you can query for a new thread.

当你问一个新的线程,它要么给你一个新的或排队的工作,直到线程得到释放。以这种方式,框架是在电荷上决定阉它应该创建多个线程或不依赖于present CPU的数量。

When you ask for a new thread it will either give you a new one or enqueue the work until a thread get freed. In that way the framework is in charge on deciding wether it should create more threads or not depending on the number of present CPUs.

编辑:此外,因为它已被已经提到的,操作系统负责不同的CPU之间分配线程

In addition, as it has been already mentioned, the OS is in charge of distributing the threads among the different CPUs.

这篇关于我如何产卵在不同的CPU内核线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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