Use of lock in Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
This paper briefly introduces the lock mechanism in Go
.

There are two types of locks in go:

    • Mutual exclusion Lock
    • Read/write Lock


Mutual exclusion Lock

The most common limitation of concurrent programs to access to public resources is the way the mutex is used. In Go, sync. Mutexes provide implementations of mutex locks.

Simple Use Example:

func Main() {var Mutex Sync.MutexCount := 0 for R := 0; R <  -; R++ {   Go func() {           Mutex.Lock()           Count += 1           Mutex.Unlock()       }()   }    Time.Sleep( Time.Second)   FMT.Println("The Count is:", Count)}

When an operation is performed mutex.Lock() , if there is another goroutine that performs a lock operation, the operation is blocked until the mutex is restored to an unlocked state.


Read/write Lock

As the name implies, read-write locks are locked for read and write operations. It is important to note that there is no mutex between multiple read operations, which increases the efficiency of access to shared resources.

The read and write lock in Go is by sync. Rwmutex provides, mainly includes:

-Func (rw *rwmutex) lock ()

-Func (rw *rwmutex) Rlock ()

-Func (rw *rwmutex) rlocker () Locker

-Func (rw *rwmutex) Runlock ()

-Func (rw *rwmutex) Unlock ()

where lock () is "write lock", after calling "write lock", there is no other goroutine to read or write. Unlock (), "Write unlock", invokes "write unlock" to wake all goroutine that are blocked because of a read lock (i.e., Rlock ()).

Rlock () is read locked, and after a read lock is called, no other goroutine can be written, but read operations are possible. Runlock () is "read unlock", and after calling "read unlock", it wakes up a goroutine that is blocked because of a "write lock".

Simple Use Example:

 Package MainImport ("FMT""Sync""Time")func Main() {    var Mutex Sync.Rwmutex    arr := []int{1, 2, 3}    Go func() {       FMT.Println("Try to lock writing operation.")       Mutex.Lock()       FMT.Println("Writing operation is locked.")       arr = Append(arr, 4)       FMT.Println("Try to unlock writing operation.")       Mutex.Unlock()       FMT.Println("Writing operation is unlocked.")   }()   Go func() {       FMT.Println("Try to lock reading operation.")       Mutex.Rlock()       FMT.Println("The reading operation is locked.")       FMT.Println("The Len of arr is:", Len(arr))       FMT.Println("Try to unlock reading operation.")       Mutex.Runlock()       FMT.Println("The reading operation is unlocked.")   }()    Time.Sleep( Time.Second * 2)   return}


Running the above example and observing the output results, you will be able to intuitively feel the role of read-write lock.

Public Number: Easyhacking

Weibo: easyhacking

Welcome to talk about

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.