what is the difference between RLock() and Lock() in Golang?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















what is the difference between RLock() and Lock() in Golang and how they can be used efficiently when we use mutex Lock ?










share|improve this question





























    -3















    what is the difference between RLock() and Lock() in Golang and how they can be used efficiently when we use mutex Lock ?










    share|improve this question

























      -3












      -3








      -3








      what is the difference between RLock() and Lock() in Golang and how they can be used efficiently when we use mutex Lock ?










      share|improve this question














      what is the difference between RLock() and Lock() in Golang and how they can be used efficiently when we use mutex Lock ?







      go mutex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 9:36









      Sharath BJSharath BJ

      58144




      58144
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Lock(): only one go routine read/write at a time by acquiring the lock.



          RLock(): multiple go routine can read(not write) at a time by acquiring the lock.



          package main

          import (
          "fmt"
          "sync"
          "time"
          )

          func main() {

          a := 0

          lock := sync.RWMutex{}

          for i := 1; i < 10; i++ {
          go func(i int) {
          lock.Lock()
          fmt.Printf("Lock: from go routine %d: a = %dn",i, a)
          time.Sleep(time.Second)
          lock.Unlock()
          }(i)
          }

          b := 0

          for i := 11; i < 20; i++ {
          go func(i int) {
          lock.RLock()
          fmt.Printf("RLock: from go routine %d: b = %dn",i, b)
          time.Sleep(time.Second)
          lock.RUnlock()
          }(i)
          }

          <-time.After(time.Second*10)
          }


          1) When a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens?




          • To acquire a Lock() for write it has to wait until RUnlock()


          2) What happens when someone already acquired Lock() for map ,will other go-routine can still get RLock()




          • if someone X already acquired Lock(), then other go-routine to get RLock() will have to wait until X release lock (Unlock())


          3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?




          • Map is not thread safe. so "concurrent read/write of Map" can cause error.


          See following example for more clarification:



          package main

          import (
          "fmt"
          "sync"
          "time"
          )

          func main() {
          lock := sync.RWMutex{}

          b := map[string]int{}
          b["0"] = 0

          go func(i int) {
          lock.RLock()
          fmt.Printf("RLock: from go routine %d: b = %dn",i, b["0"])
          time.Sleep(time.Second*3)
          fmt.Printf("RLock: from go routine %d: lock releasedn",i)
          lock.RUnlock()
          }(1)

          go func(i int) {
          lock.Lock()
          b["2"] = i
          fmt.Printf("Lock: from go routine %d: b = %dn",i, b["2"])
          time.Sleep(time.Second*3)
          fmt.Printf("Lock: from go routine %d: lock releasedn",i)
          lock.Unlock()
          }(2)

          <-time.After(time.Second*8)

          fmt.Println("*************************************8")

          go func(i int) {
          lock.Lock()
          b["3"] = i
          fmt.Printf("Lock: from go routine %d: b = %dn",i, b["3"])
          time.Sleep(time.Second*3)
          fmt.Printf("Lock: from go routine %d: lock releasedn",i)
          lock.Unlock()
          }(3)

          go func(i int) {
          lock.RLock()
          fmt.Printf("RLock: from go routine %d: b = %dn",i, b["3"])
          time.Sleep(time.Second*3)
          fmt.Printf("RLock: from go routine %d: lock releasedn",i)
          lock.RUnlock()
          }(4)

          <-time.After(time.Second*8)
          }





          share|improve this answer


























          • thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

            – Sharath BJ
            Nov 22 '18 at 11:01








          • 1





            @SharathBJ updated my answer

            – nightfury1204
            Nov 22 '18 at 11:39



















          0














          A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.



          A RWMutex must not be copied after first use.



          If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.





          A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.



          The golang provide the channel is the best practice for concurrency control, so i think the efficiently way using sync.lock is not used it, use channel instead.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427824%2fwhat-is-the-difference-between-rlock-and-lock-in-golang%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Lock(): only one go routine read/write at a time by acquiring the lock.



            RLock(): multiple go routine can read(not write) at a time by acquiring the lock.



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {

            a := 0

            lock := sync.RWMutex{}

            for i := 1; i < 10; i++ {
            go func(i int) {
            lock.Lock()
            fmt.Printf("Lock: from go routine %d: a = %dn",i, a)
            time.Sleep(time.Second)
            lock.Unlock()
            }(i)
            }

            b := 0

            for i := 11; i < 20; i++ {
            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b)
            time.Sleep(time.Second)
            lock.RUnlock()
            }(i)
            }

            <-time.After(time.Second*10)
            }


            1) When a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens?




            • To acquire a Lock() for write it has to wait until RUnlock()


            2) What happens when someone already acquired Lock() for map ,will other go-routine can still get RLock()




            • if someone X already acquired Lock(), then other go-routine to get RLock() will have to wait until X release lock (Unlock())


            3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?




            • Map is not thread safe. so "concurrent read/write of Map" can cause error.


            See following example for more clarification:



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {
            lock := sync.RWMutex{}

            b := map[string]int{}
            b["0"] = 0

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["0"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(1)

            go func(i int) {
            lock.Lock()
            b["2"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["2"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(2)

            <-time.After(time.Second*8)

            fmt.Println("*************************************8")

            go func(i int) {
            lock.Lock()
            b["3"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(3)

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(4)

            <-time.After(time.Second*8)
            }





            share|improve this answer


























            • thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

              – Sharath BJ
              Nov 22 '18 at 11:01








            • 1





              @SharathBJ updated my answer

              – nightfury1204
              Nov 22 '18 at 11:39
















            0














            Lock(): only one go routine read/write at a time by acquiring the lock.



            RLock(): multiple go routine can read(not write) at a time by acquiring the lock.



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {

            a := 0

            lock := sync.RWMutex{}

            for i := 1; i < 10; i++ {
            go func(i int) {
            lock.Lock()
            fmt.Printf("Lock: from go routine %d: a = %dn",i, a)
            time.Sleep(time.Second)
            lock.Unlock()
            }(i)
            }

            b := 0

            for i := 11; i < 20; i++ {
            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b)
            time.Sleep(time.Second)
            lock.RUnlock()
            }(i)
            }

            <-time.After(time.Second*10)
            }


            1) When a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens?




            • To acquire a Lock() for write it has to wait until RUnlock()


            2) What happens when someone already acquired Lock() for map ,will other go-routine can still get RLock()




            • if someone X already acquired Lock(), then other go-routine to get RLock() will have to wait until X release lock (Unlock())


            3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?




            • Map is not thread safe. so "concurrent read/write of Map" can cause error.


            See following example for more clarification:



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {
            lock := sync.RWMutex{}

            b := map[string]int{}
            b["0"] = 0

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["0"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(1)

            go func(i int) {
            lock.Lock()
            b["2"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["2"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(2)

            <-time.After(time.Second*8)

            fmt.Println("*************************************8")

            go func(i int) {
            lock.Lock()
            b["3"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(3)

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(4)

            <-time.After(time.Second*8)
            }





            share|improve this answer


























            • thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

              – Sharath BJ
              Nov 22 '18 at 11:01








            • 1





              @SharathBJ updated my answer

              – nightfury1204
              Nov 22 '18 at 11:39














            0












            0








            0







            Lock(): only one go routine read/write at a time by acquiring the lock.



            RLock(): multiple go routine can read(not write) at a time by acquiring the lock.



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {

            a := 0

            lock := sync.RWMutex{}

            for i := 1; i < 10; i++ {
            go func(i int) {
            lock.Lock()
            fmt.Printf("Lock: from go routine %d: a = %dn",i, a)
            time.Sleep(time.Second)
            lock.Unlock()
            }(i)
            }

            b := 0

            for i := 11; i < 20; i++ {
            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b)
            time.Sleep(time.Second)
            lock.RUnlock()
            }(i)
            }

            <-time.After(time.Second*10)
            }


            1) When a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens?




            • To acquire a Lock() for write it has to wait until RUnlock()


            2) What happens when someone already acquired Lock() for map ,will other go-routine can still get RLock()




            • if someone X already acquired Lock(), then other go-routine to get RLock() will have to wait until X release lock (Unlock())


            3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?




            • Map is not thread safe. so "concurrent read/write of Map" can cause error.


            See following example for more clarification:



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {
            lock := sync.RWMutex{}

            b := map[string]int{}
            b["0"] = 0

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["0"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(1)

            go func(i int) {
            lock.Lock()
            b["2"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["2"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(2)

            <-time.After(time.Second*8)

            fmt.Println("*************************************8")

            go func(i int) {
            lock.Lock()
            b["3"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(3)

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(4)

            <-time.After(time.Second*8)
            }





            share|improve this answer















            Lock(): only one go routine read/write at a time by acquiring the lock.



            RLock(): multiple go routine can read(not write) at a time by acquiring the lock.



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {

            a := 0

            lock := sync.RWMutex{}

            for i := 1; i < 10; i++ {
            go func(i int) {
            lock.Lock()
            fmt.Printf("Lock: from go routine %d: a = %dn",i, a)
            time.Sleep(time.Second)
            lock.Unlock()
            }(i)
            }

            b := 0

            for i := 11; i < 20; i++ {
            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b)
            time.Sleep(time.Second)
            lock.RUnlock()
            }(i)
            }

            <-time.After(time.Second*10)
            }


            1) When a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens?




            • To acquire a Lock() for write it has to wait until RUnlock()


            2) What happens when someone already acquired Lock() for map ,will other go-routine can still get RLock()




            • if someone X already acquired Lock(), then other go-routine to get RLock() will have to wait until X release lock (Unlock())


            3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?




            • Map is not thread safe. so "concurrent read/write of Map" can cause error.


            See following example for more clarification:



            package main

            import (
            "fmt"
            "sync"
            "time"
            )

            func main() {
            lock := sync.RWMutex{}

            b := map[string]int{}
            b["0"] = 0

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["0"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(1)

            go func(i int) {
            lock.Lock()
            b["2"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["2"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(2)

            <-time.After(time.Second*8)

            fmt.Println("*************************************8")

            go func(i int) {
            lock.Lock()
            b["3"] = i
            fmt.Printf("Lock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("Lock: from go routine %d: lock releasedn",i)
            lock.Unlock()
            }(3)

            go func(i int) {
            lock.RLock()
            fmt.Printf("RLock: from go routine %d: b = %dn",i, b["3"])
            time.Sleep(time.Second*3)
            fmt.Printf("RLock: from go routine %d: lock releasedn",i)
            lock.RUnlock()
            }(4)

            <-time.After(time.Second*8)
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 11:39

























            answered Nov 22 '18 at 10:50









            nightfury1204nightfury1204

            1,779510




            1,779510













            • thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

              – Sharath BJ
              Nov 22 '18 at 11:01








            • 1





              @SharathBJ updated my answer

              – nightfury1204
              Nov 22 '18 at 11:39



















            • thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

              – Sharath BJ
              Nov 22 '18 at 11:01








            • 1





              @SharathBJ updated my answer

              – nightfury1204
              Nov 22 '18 at 11:39

















            thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

            – Sharath BJ
            Nov 22 '18 at 11:01







            thanks for the answer ,have some question 1)when a go-routine has already acquired a RLock(), can another go-routine acquire a Lock() for write or it has to wait until RUnlock() happens? 2) what happens when someone already acquired Lock() for map ,will other go-routine can still get RLock() 3) Assuming we are dealing with Maps here, is there any possibility of "concurrent read/write of Map" error can come?

            – Sharath BJ
            Nov 22 '18 at 11:01






            1




            1





            @SharathBJ updated my answer

            – nightfury1204
            Nov 22 '18 at 11:39





            @SharathBJ updated my answer

            – nightfury1204
            Nov 22 '18 at 11:39













            0














            A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.



            A RWMutex must not be copied after first use.



            If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.





            A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.



            The golang provide the channel is the best practice for concurrency control, so i think the efficiently way using sync.lock is not used it, use channel instead.






            share|improve this answer




























              0














              A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.



              A RWMutex must not be copied after first use.



              If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.





              A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.



              The golang provide the channel is the best practice for concurrency control, so i think the efficiently way using sync.lock is not used it, use channel instead.






              share|improve this answer


























                0












                0








                0







                A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.



                A RWMutex must not be copied after first use.



                If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.





                A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.



                The golang provide the channel is the best practice for concurrency control, so i think the efficiently way using sync.lock is not used it, use channel instead.






                share|improve this answer













                A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer. The zero value for a RWMutex is an unlocked mutex.



                A RWMutex must not be copied after first use.



                If a goroutine holds a RWMutex for reading and another goroutine might call Lock, no goroutine should expect to be able to acquire a read lock until the initial read lock is released. In particular, this prohibits recursive read locking. This is to ensure that the lock eventually becomes available; a blocked Lock call excludes new readers from acquiring the lock.





                A Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked mutex.



                The golang provide the channel is the best practice for concurrency control, so i think the efficiently way using sync.lock is not used it, use channel instead.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 9:50









                L.HaoL.Hao

                71




                71






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427824%2fwhat-is-the-difference-between-rlock-and-lock-in-golang%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?