How do I spawn threads on different CPU cores?

后端 未结 10 2038
梦谈多话
梦谈多话 2020-11-28 20:43

Let\'s say I had a program in C# that did something computationally expensive, like encoding a list of WAV files into MP3s. Ordinarily I would encode the files one at a time

10条回答
  •  借酒劲吻你
    2020-11-28 21:33

    Don't bother doing that.

    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.

    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.

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

提交回复
热议问题