dongyuan6949
dongyuan6949
2017-01-17 12:52

防止错误共享的奇怪代码

  • mutex
  • multithreading

I want to discuss the following structure in golang from this link

// Local per-P Pool appendix.
    57  type poolLocal struct {
    58      private interface{}   // Can be used only by the respective P.
    59      shared  []interface{} // Can be used by any P.
    60      Mutex                 // Protects shared.
    61      pad     [128]byte     // Prevents false sharing.
    62  }

The above structure can be accessed only one thread at a time as Mutex is used. The coder will Lock the structure in the beginning of a thread and unlock it when the thread completes. So the memory is not shared between threads. So no more than one core will have access to the memory. So, by my understanding, false sharing cannot happen here. If false sharing cannot happen, why did the coder pad the structure with extra bytes (pad [128]byte) ? Is my understanding wrong?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐