<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                golang中sync包實現了兩種鎖Mutex (互斥鎖)和RWMutex(讀寫鎖),其中RWMutex是基于Mutex實現的,只讀鎖的實現使用類似引用計數器的功能。 1、互斥鎖 (用于基本上全是寫入操作的應用) ~~~ type Mutex func (m *Mutex) Lock() func (m *Mutex) Unlock() ~~~ go mutex是互斥鎖,只有Lock和Unlock兩個方法,在這兩個方法之間的代碼不能被多個goroutins同時調用到。 其中Mutex為互斥鎖,Lock()加鎖,Unlock()解鎖,使用Lock()加鎖后,便不能再次對其進行加鎖,直到利用Unlock()解鎖對其解鎖后,才能再次加鎖.適用于讀寫不確定場景,即讀寫次數沒有明顯的區別,并且只允許只有一個讀或者寫的場景,所以該鎖葉叫做全局鎖。 func (m *Mutex) Unlock()用于解鎖m,如果在使用Unlock()前未加鎖,就會引起一個運行錯誤.已經鎖定的Mutex并不與特定的goroutine相關聯,這樣可以利用一個goroutine對其加鎖,再利用其他goroutine對其解鎖。 互斥鎖只能鎖定一次,當在解鎖之前再次進行加鎖,便會死鎖狀態,如果在加鎖前解鎖,便會報錯“panic: sync: unlock of unlocked mutex” 同一時刻只有一個攜程在操作: ~~~ package main import ( "fmt" "sync" "time" ) var ( m = make(map[int]uint64) lock sync.Mutex //互斥鎖 ) type task struct { n int } func calc(t *task) { var sum uint64 sum = 1 for i := 1; i < t.n; i++ { sum *= uint64(i) } fmt.Println(t.n, sum) // lock.Lock() m[t.n] = sum // lock.Unlock() } func main() { for i := 0; i < 16; i++ { t := &task{n: i} go calc(t) } time.Sleep(10 * time.Second) // lock.Lock() for k, v := range m { fmt.Printf("%d! = %v\n", k, v) } // lock.Unlock() } ~~~ 輸出結果:(偶爾也會正常執行,多執行幾次) ~~~ 1 1 2 1 7 720 fatal error: concurrent map writes fatal error: concurrent map writes goroutine 7 [running]: runtime.throw(0x10c80e7, 0x15) ~~~ ~~~ package main import ( "fmt" "sync" "time" ) var ( m = make(map[int]uint64) lock sync.Mutex ) type task struct { n int } func calc(t *task) { var sum uint64 sum = 1 for i := 1; i < t.n; i++ { sum *= uint64(i) } fmt.Println(t.n, sum) lock.Lock() m[t.n] = sum lock.Unlock() } func main() { for i := 0; i < 16; i++ { t := &task{n: i} go calc(t) } time.Sleep(10 * time.Second) lock.Lock() for k, v := range m { fmt.Printf("%d! = %v\n", k, v) } lock.Unlock() } ~~~ 輸出結果: ~~~ 4 6 3 2 10 362880 5 24 2 1 8 5040 1 1 9 40320 0 1 6 120 13 479001600 14 6227020800 7 720 15 87178291200 12 39916800 11 3628800 2! = 1 6! = 120 15! = 87178291200 3! = 2 10! = 362880 1! = 1 4! = 6 8! = 5040 14! = 6227020800 11! = 3628800 9! = 40320 0! = 1 7! = 720 12! = 39916800 5! = 24 13! = 479001600 ~~~ 建議:同一個互斥鎖的成對鎖定和解鎖操作放在同一層次的代碼塊中。 ~~~ package main import ( "fmt" "sync" "time" ) func main(){ //聲明 var mutex sync.Mutex fmt.Println("Lock the lock. (G0)") //加鎖mutex mutex.Lock() fmt.Println("The lock is locked.(G0)") for i := 1; i < 4; i++ { go func(i int) { fmt.Printf("Lock the lock.(G%d)\r\n", i) mutex.Lock() fmt.Printf("The lock is locked.(G%d)\r\n", i) }(i) } time.Sleep(time.Second) fmt.Println("Unlock the lock. (G0)") //解鎖mutex mutex.Unlock() fmt.Println("The lock is unlocked. (G0)") time.Sleep(time.Second) } ~~~ 輸出結果: ~~~ Lock the lock. (G0) The lock is locked.(G0) Lock the lock.(G3) Lock the lock.(G2) Lock the lock.(G1) Unlock the lock. (G0) The lock is unlocked. (G0) The lock is locked.(G3) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看