2014年6月1日日曜日

Synchronization patterns in Go

When I write codes for ik, often I find it necessary to synchronize the access to some resource from a number of goroutines. While using channels almost looks like the way to “go” , simply applying mutexes also has some clear benefits. I think it is a good time to do some comparison over the two, so here it goes:

Good-old mutexes

Procedures that manipulate on the shared resource are surrounded by mutex calls. It should be easy to follow the control flow because the callgraph from the initiator of the action to the critical sections is pretty much straight-forward, and so it is to do the function-level testing as everything is serialized in a single flow and all you need is call the target function.

But it'd generally turn out to be a nightmare to find out the cause of the deadlock where three or more locks are involved.

Daemon pattern

Access to the resource is restricted to just one goroutine and every initiator of the action is required to send the message to the goroutine to let it manipulate the resource. The result of the manipulation is retrieved through the channel too. This is what I call “daemon pattern” as the manipulating goroutine behaves analogously to Unix's service daemons. I don't know the exact name of the pattern but there should be a good name for it.

One of the notable benefit over mutexes is that you can identify what causes a deadlock much easier because synchronization happens only when the communication takes place. In addition, it enables you to implement timeout with another channel without much difficulty.

But you'd end up managing the lifetime of the daemon goroutine as it needs to be restarted when it aborts for some reasons and needs to be stopped when it is no longer necessary. Besides it'd be harder to figure out the control flow because of the separated callgraphs and extra codes for the communication. Thus the code for function-level testing would be complicated.

Summary

  • Mutexes

    • Pros

      • Straight-forward, top-down callgraph
      • Easily testable in a single thread
    • Cons

      • Terribly hard to find out the deadlock site
  • Daemon pattern

    • Pros

      • Comparatively easier to find deadlocks because locks are far more obvious than mutexes
      • Need to manage lifetime of daemon goroutines
    • Cons:

      • Testing would be a little bit difficult

P.S. Go wiki has the dedicated page for this topic: Mutex or Channel

2 件のコメント:

  1. Thanks for sharing, nice post!

    Máy đưa võng hay vong em be tu dong hay gia may dua vong tu dong giúp bé ngủ ngon mà võng đưa tự động không tốn sức ru võng. Võng tự động hay võng đưa tự động chắc chắn, gọn gàng, dễ tháo xếp, dễ di chuyển và may dua vong ts dễ dàng bảo quản. Lợi ích mà máy đưa võng tự động vô cùng thiết thực.
    Chia sẻ các bạn làm thế nào để trẻ thích đọc sách hay Collagen trị mụn được không hay chữa mất ngủ bằng gừng đơn giản, bí quyết làm trắng da bằng cà phê và dầu dừa hiệu quả hay cách giúp trẻ không đái dầm ban đêm hiệu quả hay giảm cân nhanh bằng gạo lứt hq hay mẹo giúp tăng cường trí nhớ hiệu quả, kinh nghiệm trị tiêu chảy cho bé bằng cà rốt hiệu quả, những thực phẩm giúp cải thiện trí nhớ hiệu quả, mẹo hay giúp trẻ thích ăn rau hay cách giúp trẻ hạ sốt nhanh hiệu quả, bệnh viêm khớp không nên ăn gì hay mẹo giúp giảm độ cận thị cho bạn, bí quyết chống nắng với cà chua cực hiệu quả, cách giúp bé ngủ ngon giấcthực phẩm giúp bé ngủ ngon mẹ nên biết, chia sẻ cách làm trắng da toàn thân bằng thực phẩm, những món ăn chữa bệnh mất ngủ hay mách mẹ mẹo giúp bé không sốt khi mọc răng hiệu quả
    Những thực phẩm giúp đẹp da tại http://nhungthucphamgiupda.blogspot.com/
    Thực phẩm giúp bạn trẻ đẹp tại http://thucphamgiuptre.blogspot.com/
    Thực phẩm làm tăng tại http://thucphamlamtang.blogspot.com/
    Những thực phẩm giúp làm giảm tại http://thucphamlamgiam.blogspot.com/

    返信削除
  2. Bạn nên biết về cach lam thach rau cau 3d
    Thạch rau cau nâng cao sẽ có tại cach lam thach rau cau nhieu tang
    Bạn sẽ vô cùng ngạc nhiên với cach lam thach rau cau tai nha
    Hãy làm điều gì đó ý nghĩa khi chọn qua tang valentine y nghia cho nguoi yeu
    Đừng bao giờ bỏ qua qua tang valentine y nghia cho ban gai
    Bạn sẽ thích những món qua tang valentine y nghia nhat
    Cùng tìm hiểu stt tam trang ve tinh yeu cuoc song
    Giúp tình yêu thêm nồng nàng với nhung stt tinh yeu hay nhat
    Đôi khi bạn buồn thì hãy đến với stt tam trang buon co don

    返信削除

Blogger Syntax Highliter

フォロワー