Home  >  Article  >  Backend Development  >  How to concurrently golang

How to concurrently golang

WBOY
WBOYOriginal
2023-05-19 09:28:37647browse

In modern software development, concurrent programming has become one of the necessary skills. Go language is a language specially designed for concurrent programming. Its built-in Goroutines, Channels, Mutex and other mechanisms can greatly simplify the complexity of concurrent programming. This article will introduce how to perform concurrent programming in the Go language, providing you with basic knowledge and practical skills so that you can write concurrent programs more efficiently.

1. What is concurrent programming

Usually, we have multiple tasks to process in software, such as reading multiple files at the same time or getting data from multiple databases. Concurrent programming is to make multiple parts of the program execute at the same time to improve the efficiency of the program in processing tasks. In concurrent programming, we need to avoid conflicts between multiple tasks and ensure the correctness and maintainability of the code.

2. Goroutines and Channels

Goroutines and Channels in the Go language are the core mechanisms for implementing concurrent programming.

Goroutines are lightweight threads in the Go language. Starting a new Goroutine through the keyword go allows the program to perform multiple tasks at the same time. Goroutine can execute in the same address space and can easily switch threads without the overhead of creating threads, destroying threads, and context switching like operating system threads.

Channel is a way of messaging between Goroutines in the Go language. Through Channels, Goroutines can exchange data safely and avoid data races. Channel can be regarded as a pipeline, which sends messages (data) in one Goroutine and receives messages (data) in another Goroutine, realizing communication between two Goroutines.

The following is a simple example using Goroutines and Channels to implement concurrent calculations:

package main

import (
    "fmt"
    "time"
)

func main() {
    start := time.Now()
    c := make(chan int)
    go calculateSum(c)
    sum := <-c
    end := time.Now()
    fmt.Printf("Sum: %v
Time: %v", sum, end.Sub(start))
}

func calculateSum(c chan int) {
    sum := 0
    for i := 0; i < 1000000000; i++ {
        sum += i
    }
    c <- sum
}

In this example, we start a new Goroutine to calculate the cumulative sum from 0 to 100 million , and pass the result to the main Goroutine through Channel. By using Goroutines and Channels, we can perform other tasks during the calculation process and improve the efficiency of the program.

3. Mutex

In concurrent programming, we need to control access to shared resources to avoid data competition and errors. The Go language provides Mutex (mutex lock) to implement safe access control to shared resources. Mutex can ensure that only one Goroutine can access shared resources at the same time, and other Goroutines must wait for the Mutex to be unlocked before they can continue to access.

The following is an example of using Mutex to ensure safe access to shared variables:

package main

import (
    "fmt"
    "sync"
)

var count int
var wg sync.WaitGroup
var mutex sync.Mutex

func main() {
    wg.Add(2)
    go increment()
    go increment()
    wg.Wait()
    fmt.Println("Count:", count)
}

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

In this example, we start two Goroutines and increment the shared count variable 1 million times operate. Before each Goroutine increments the count variable, we use a Mutex to lock the variable to ensure that only one Goroutine can access the variable at the same time.

4. Use semaphores to control concurrency

In addition to using Mutex to control access to shared resources, we can also use semaphores to control concurrency to prevent the system from crashing due to excessive concurrency.

Semaphore is a classic concurrent programming technology in the operating system. It can control the number of shared resources accessed at the same time, unlike Mutex, which can only be accessed by one Goroutine at any time. The sync package in the Go language provides support for semaphores, which can easily control the number of concurrent Goroutines.

The following is an example of using a semaphore to control the concurrent number of Goroutines:

package main

import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup
var semaphore = make(chan struct{}, 2) // 最多允许2个Goroutine并发访问

func main() {
    for i := 0; i < 5; i++ { // 开启5个任务
        wg.Add(1)
        go runTask(i)
    }
    wg.Wait()
}

func runTask(i int) {
    defer wg.Done()
    semaphore <- struct{}{} // 加锁,占用1个slot
    defer func() {
        <-semaphore // 解锁,释放1个slot
    }()

    fmt.Printf("Task %d start
", i)
    // do something
    fmt.Printf("Task %d end
", i)
}

In this example, we use a Channel with a capacity of 2 as the semaphore, which can allow up to 2 Goroutines. run simultaneously. Each Goroutine obtains a semaphore slot when starting a task, and releases the semaphore after completing the task. When there are already two Goroutines running, subsequent Goroutines must wait for the existing tasks to complete and release the semaphore before they can start running.

By using semaphores, we can effectively control the number of concurrent tasks and avoid resource shortage or crash caused by excessive system concurrency.

5. Summary

Concurrent programming is an essential skill in modern software development. Go language provides simple and easy-to-use Goroutines and Channels mechanisms, as well as advanced mechanisms such as Mutex and semaphores. It is convenient for us to achieve efficient and safe concurrent programming. When using these mechanisms, we need to pay attention to the management of data competition and shared resources to ensure the correctness and maintainability of the program. Through the introduction of this article, you should already know how to use mechanisms such as Goroutines, Channels, Mutex, and semaphores for concurrent programming, and be able to write efficient and safe concurrent programs.

The above is the detailed content of How to concurrently golang. 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
Previous article:How to model in golangNext article:How to model in golang