Home Backend Development Golang Revealing the operating mechanism of locks in Golang

Revealing the operating mechanism of locks in Golang

Jan 24, 2024 am 08:57 AM
golang working principle Lock

Revealing the operating mechanism of locks in Golang

Exploration on the working principle of locks in Golang

In concurrent programming, locks are an important synchronization mechanism used to protect access to shared resources. Golang provides lock support through the built-in sync package, allowing us to safely share data between multiple goroutines. This article will delve into the working principle of locks in Golang and explain it with specific code examples.

1. Mutex lock

The most basic lock type in Golang is the mutex lock (Mutex), which is represented by the Mutex structure in the sync package. The principle of a mutex lock is simple: when a goroutine accesses a shared resource, it will first lock the resource, and other goroutines need to wait for the lock to be released before they can access it. The use of mutex locks is very easy. Just call the Lock() method to lock the resource and the Unlock() method to release the lock.

The following is a simple example that demonstrates the process of two goroutines accessing shared resources:

package main

import (
    "fmt"
    "sync"
)

var count int
var mutex sync.Mutex

func main() {
    wg := sync.WaitGroup{}
    wg.Add(2)

    go increment()
    go increment()

    wg.Wait()

    fmt.Println("Final count:", count)
}

func increment() {
    for i := 0; i < 100000; i++ {
        mutex.Lock()
        count++
        mutex.Unlock()
    }
    wg.Done()
}
Copy after login

In the above example, we defined a global variable count to represent shared resources. In addition, a mutex lock mutex is defined. In the increment() function in the two goroutines, we use the mutex.Lock() method to lock the shared resource count, and then call the mutex.Unlock() method to release the lock after performing the count operation. Finally, we use sync.WaitGroup to ensure that the final count value is printed after the two goroutines are executed.

The working principle of the mutex lock is very simple and clear. It uses the locking and unlocking mechanism to ensure safe access to shared resources and avoid data competition.

2. Read-write lock

In some scenarios, mutex locks will cause performance bottlenecks. If multiple goroutines only read shared resources without performing write operations, there is no need to lock at all. In order to improve concurrency performance, Golang provides read-write locks (RWMutex). Read-write locks allow multiple goroutines to read shared resources at the same time, but they require mutually exclusive access when there are write operations.

The use of read-write locks is very simple and is represented by the RWMutex structure in the sync package. When reading shared resources, call the RLock() method to add a read lock, when writing to a shared resource, call the Lock() method to add a write lock, and when releasing the lock, call the RUnlock() and Unlock() methods respectively.

The following is a simple example that demonstrates the use of read-write locks:

package main

import (
    "fmt"
    "sync"
)

var count int
var rwlock sync.RWMutex

func main() {
    wg := sync.WaitGroup{}
    wg.Add(3)

    go increment()
    go readCount()
    go readCount()

    wg.Wait()
}

func increment() {
    for i := 0; i < 100000; i++ {
        rwlock.Lock()
        count++
        rwlock.Unlock()
    }
    wg.Done()
}

func readCount() {
    rwlock.RLock()
    fmt.Println("Current count:", count)
    rwlock.RUnlock()
    wg.Done()
}
Copy after login

In the above example, we use a global variable count to represent shared resources, and also define a read-write Lock rwlock. In the increment() function, we use the rwlock.Lock() method to add the write lock, and then call the rwlock.Unlock() method to release the lock after performing the count operation. In the readCount() function, we use the rwlock.RLock() method to add the read lock, print the current value of count, and then call the rwlock.RUnlock() method to release the lock. Through the use of read-write locks, we can achieve multiple goroutines to read the value of count at the same time without blocking, which greatly improves the concurrency of read operations.

3. Condition variables

In addition to mutex locks and read-write locks, Golang also provides condition variables (Cond) to further optimize concurrent programming. Condition variables allow goroutine to wait when a certain condition is met and then continue execution until the condition changes.

The use of condition variables is very flexible and is represented by the Cond structure in the sync package. We can wait for the condition to be met by calling Cond's Wait() method, and call Cond's Signal() method or Broadcast() method to wake up the waiting goroutine.

The following is a simple example that demonstrates the use of condition variables:

package main

import (
    "fmt"
    "sync"
)

var count int
var cond *sync.Cond

func main() {
    cond = sync.NewCond(&sync.Mutex{})
    wg := sync.WaitGroup{}
    wg.Add(3)

    go increment()
    go decrement()
    go waitCount()

    wg.Wait()
}

func increment() {
    for i := 0; i < 10; i++ {
        cond.L.Lock()
        count++
        fmt.Println("Increment count to", count)
        cond.Signal()
        cond.L.Unlock()
    }
    wg.Done()
}

func decrement() {
    for i := 0; i < 5; i++ {
        cond.L.Lock()
        for count <= 0 {
            cond.Wait()
        }
        count--
        fmt.Println("Decrement count to", count)
        cond.L.Unlock()
    }
    wg.Done()
}

func waitCount() {
    cond.L.Lock()
    for count < 5 {
        cond.Wait()
    }
    fmt.Println("Count reaches 5")
    cond.L.Unlock()
    wg.Done()
}
Copy after login

In the above example, we use a global variable count to represent shared resources, and also define a condition variable cond , create a condition variable associated with the mutex lock by calling the sync.NewCond() method.

In the increment() function, we first acquire the lock of the mutex cond.L, then perform the count operation, print the current count value, and finally call the cond.Signal() method to wake up the waiting goroutine. In the decrement() function, we first obtain the lock of the mutex cond.L, and then use the for loop to determine whether the count is less than or equal to 0. If so, call the cond.Wait() method to suspend the current goroutine and wait for the conditions to be met. When count is greater than 0, perform the count-- operation, print the current count value, and finally release the mutex lock. In the waitCount() function, we first obtain the lock of the mutex cond.L, and then use the for loop to determine whether the count is less than 5. If so, call the cond.Wait() method to suspend the current goroutine and wait for the conditions to be met. When count reaches 5, print the prompt message "Count reaches 5", and finally release the mutex lock.

Through the use of condition variables, we can achieve more complex inter-thread communication than mutex locks and read-write locks, and more flexibly control the execution order of goroutine.

Summary:

This article deeply explores the working principle of locks in Golang, including the use of mutex locks, read-write locks and condition variables. Mutex locks ensure safe access to shared resources through locking and unlocking. Read-write locks improve concurrency performance through read locks and write locks. Condition variables allow goroutine to wait when a certain condition is met. Through the appropriate use of locks, we can improve the performance of the program and ensure that shared resources are shared correctly among multiple goroutines.

The above is the detailed content of Revealing the operating mechanism of locks in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1259
29
C# Tutorial
1233
24
How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Golang framework development practical tutorial: FAQs Golang framework development practical tutorial: FAQs Jun 06, 2024 am 11:02 AM

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

How to find the first substring matched by a Golang regular expression? How to find the first substring matched by a Golang regular expression? Jun 06, 2024 am 10:51 AM

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

See all articles