douqiang1851 2009-11-16 02:49
浏览 29
已采纳

Go和Java使用用户空间线程这一事实是否就意味着您无法真正利用多核?

We've been talking about threads in my operating system class a lot lately and one question has come to my mind.

Since Go, (and Java) uses User-space thread instead of kernel threads, doesn't that mean that you can't effectively take advantages of multiple cores since the OS only allocates CPU time to the process and not the threads themselves?

This seems to confirm the fact that you can't

Wikipedia also seems to think so

  • 写回答

3条回答 默认 最新

  • dqf2015 2009-11-16 03:15
    关注

    What makes you think Go uses User-space threads?

    It doesn't. It uses OS-threads and can take advantage of multiple cores.

    You might be puzzled by the fact that by default Go only uses 1 thread to run your program. If you start two goroutines they run in one thread. But if one goroutine blocks for I/O Go creates a second thread and continues to run the other goroutine on the new thread.

    If you really want to unlock the full multi-core power just use the GOMAXPROCS() function.

    runtime.GOMAXPROCS(4); //somewhere in main

    Now your program would use 4 OS-threads (instead of 1) and would be able to fully use a e.g. 4 core system.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答