美文网首页
A Tour of GO (5) -- Concurrent

A Tour of GO (5) -- Concurrent

作者: 陈胖子胖胖胖 | 来源:发表于2016-01-13 12:06 被阅读0次
    1. Goroutine:
    • A lightweight thread managed by the GO runtime.
      go f(x, y, z)
      starts a new goroutine ruing f(x,y,z).
    • The evaluation of x, y,z happens in the current goroutine and f happens in a new goroutine.
    • The share the same memory space. Be aware of the synchronization.
    1. Channel
    • A typed conduit through which you can send and receive values with the channel operator `
      ch <- v // send v to channel ch.
      v: = <- ch //receive from ch, and assign to v.
    • Channels must be make before use
      ch: = make(chan int).
    • Sends and receives block until the other side is ready. This allow goroutines to synchronize.
    • Buffered Channels:
      ch:= make (chan int, 100)
      • Buffered channel block only when the buffer is full (for sending) and empty(for reading).
    • Range and Close: A sender can close a channel to indicate no more to send.
      • Receiver can test whether a channel has been closed by assigning a second parameter.
        v, ok :=<- ch
        ok is false if the channel is closed.
      • The loop
        for i:= range
        receives values from the channel repeatedly until it is closed.
    1. Close
      • The select lets a goroutine wait on multiple communication operations.
    • The select block until one of its case can run
    • It choose one at random if more than one is ready.
    • Default: The default case in a select is run if no other case is ready.
    1. sync.Mutex
       mux sync.Mutex
       mux.Lock()  
       mux.Unlock()

    相关文章

      网友评论

          本文标题:A Tour of GO (5) -- Concurrent

          本文链接:https://www.haomeiwen.com/subject/igyckttx.html