douzen3516 2017-01-14 17:04
浏览 52

如果程序具有更多线程,我的程序会获得更多的cpu时间吗?

If currently kernel is scheduling 60 threads, they belong to 3 processes:

A: 10 threads

B: 20 threads

C: 30 threads

And they are all doing computing (no disk IO)

Will C gets more stuff done than B, and B gets more stuff done than A?

It does not seem very fair to me. If I am a irresponsible programmer, I can just spawn more threads to eat more CPU resource.

How this relate to golang: In go, typically the go scheduler has a thread pool of #of-CPU-cores threads. Why does this make sense if a process with more threads gets more stuff done?

  • 写回答

1条回答 默认 最新

  • duai5344 2017-01-14 17:11
    关注

    It does not seem very fair to me. If I am a irresponsible programmer, I can just spawn more threads to eat more CPU resource.

    you can also allocate your entire free memory, cause OS-failure and hijack the network card. you can do sorts of things but then who will want to consume your softwares?

    How this relate to golang: In go, typically the go scheduler has a thread pool of #of-CPU-cores threads. Why does this make sense if a process with more threads gets more stuff done?

    Golang goroutines are basically a threadpool. each goroutine is a thread-pool work item. many things can make your thread-pool thread block, like using synchronous IO, waiting on a (non-spin-lock) lock and manually sleeping or yielding. in these cases, which are very common, having more threads than CPU's usually increase the performance of your application.
    Do note that not all IO is a disk IO. writing to your console is an IO operation, but it isn't really a "Disk IO".

    another thing is that context switching may not consume a large portion of your CPU, and having more threads may not make your tasks-throughput degrade. so in this case having more threads means that your parallelism is higher, and yet you don't loose performance. this is somewhat common situation. context switching between threads these days is very cheap. having a bit more threads than your cores may not necessary kill your performance or degrade it in some way.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大