Home  >  Article  >  Backend Development  >  Golang language features revealed: parallel computing and concurrency model

Golang language features revealed: parallel computing and concurrency model

王林
王林Original
2023-07-18 23:39:26938browse

Golang language features revealed: Parallel computing and concurrency model

Go language (Golang) is an open source programming language developed by Google and is famous for its simple and efficient design and excellent concurrency support. In this article, we will explore Golang’s parallel computing and concurrency models and how they can be used to improve the performance of your programs.

1. Parallel Computing

Parallel computing refers to the ability to perform multiple computing tasks at the same time. It speeds up program execution by utilizing multiple processors or processor cores. In Golang, we can use goroutines and channels to implement parallel computing.

  1. Goroutine

Goroutine is a lightweight execution unit in Golang, which can be executed concurrently with other Goroutines. Compared with operating system threads, Goroutines are started and destroyed faster and take up less memory. A Goroutine can be created using the keyword go.

The following is a sample code that uses Goroutine to calculate the Fibonacci sequence:

package main

import (
    "fmt"
)

func Fibonacci(n int, c chan int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        c <- x
        x, y = y, x+y
    }
    close(c)
}

func main() {
    c := make(chan int)
    go Fibonacci(10, c)
    for i := range c {
        fmt.Println(i)
    }
}

In the above example, we created a Goroutine to calculate the Fibonacci sequence and pass it through the channel Send the calculation results to the main thread. The main thread reads data from the channel through the range keyword and prints it to the console.

  1. Channel

Channel can be used as a pipeline for communication between Goroutines. It provides a safe concurrent access mechanism that prevents multiple Goroutines from accessing and modifying shared data at the same time.

In Golang, you can use the make function to create a channel. Channels can be buffered or unbuffered. Buffered channels can store a certain amount of data, while unbuffered channels can only store one piece of data at a time.

The following is a sample code that uses channels for parallel calculations:

package main

import (
    "fmt"
    "time"
)

func calculateSquare(number int, c chan int) {
    time.Sleep(1 * time.Second)
    c <- number * number
}

func main() {
    startTime := time.Now()

    c := make(chan int)

    for i := 1; i <= 3; i++ {
        go calculateSquare(i, c)
    }

    for i := 1; i <= 3; i++ {
        fmt.Println(<-c)
    }

    endTime := time.Now()
    elapsedTime := endTime.Sub(startTime)
    fmt.Printf("总计算时间:%s
", elapsedTime)
}

In the above example, we create a function that calculates the square and sends the calculation results to the channel. Then, 3 Goroutines are created in the main thread to perform calculation tasks concurrently, and the results are read into the main thread through the channel for printing. Finally, we use the time package to calculate and print the total computation time of the program.

2. Concurrency model

Concurrency means that multiple tasks are executed in an alternating manner, but not necessarily at the same time. A concurrency model is a method for managing and scheduling multiple concurrent tasks. In Golang, we can use mutex locks (Mutex) and read-write locks (RWMutex) to achieve concurrent data access.

  1. Mutex (Mutex)

Mutex is used to protect shared resources. Only one Goroutine is allowed to access shared resources. Other Goroutines must wait for the release of the mutex. to access. Mutex locks can be created using the Mutex type from the sync package.

The following is a sample code that uses a mutex lock to implement concurrent access to shared resources:

package main

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

var count int
var mutex sync.Mutex

func increment() {
    mutex.Lock()
    defer mutex.Unlock()
    count++
    fmt.Println(count)
}

func main() {
    for i := 0; i < 10; i++ {
        go increment()
    }

    time.Sleep(2 * time.Second)
    fmt.Printf("最终值:%d
", count)
}

In the above example, we created a global variable count and used a mutex lock to protect it Concurrent access. In each Goroutine, first use the Lock method to obtain the mutex lock, and then use the Unlock method to release the mutex lock after the function ends. Finally, we print out the final value of count.

  1. Read-write lock (RWMutex)

Read-write lock is used to handle reading and writing operations on shared resources. Unlike a mutex, multiple Goroutines can access a shared resource simultaneously for read operations, but for write operations, only one Goroutine can access it. Read-write locks can be created using the RWMutex type from the sync package.

The following is a sample code that uses read-write locks to implement concurrent reading and writing of shared resources:

package main

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

var count int
var rwMutex sync.RWMutex

func read() {
    rwMutex.RLock()
    defer rwMutex.RUnlock()
    fmt.Println(count)
}

func write() {
    rwMutex.Lock()
    defer rwMutex.Unlock()
    count++
}

func main() {
    for i := 0; i < 10; i++ {
        go read()
        go write()
    }

    time.Sleep(2 * time.Second)
}

In the above example, we created a global variable count and used read-write lock protection Its concurrent reading and writing. In each Goroutine, we use the RLock method to obtain the read lock for read operations, and the Lock method to obtain the write lock for write operations. Finally, we use the time package to ensure that the Goroutine has enough time to execute.

Summary:

Golang provides powerful parallel computing and concurrency model support, allowing us to better utilize the performance of multi-core processors and processor cores. By using Goroutines and channels to implement parallel computing, we can implement efficient concurrent programs quickly and easily. Using mutex locks and read-write locks to manage concurrent access to shared resources can ensure data consistency and reliability. By understanding and using Golang's parallel computing and concurrency model, we can better improve the performance and responsiveness of our programs.

The above is the detailed content of Golang language features revealed: parallel computing and concurrency model. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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