Go - prevent race condition


  • Prevent race condition
    • Initialize variables when package initialization phase, it will happen before main function. And don't modify those variables after initialization.
    • Avoid modifying variables by multiple goroutine => Every goroutine take care of different variable modification.
    • Dont communicate by sharing memory, share memory by communicating
    • Can pass variable address to pipeline to confine goroutine to modify variable
    • Confine variables to different stages
    • Allow many goroutines to access one variable, but only one access variable at a time



別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...