CentOS上Golang并发编程实践指南
Golang以“原生并发”为核心优势,其轻量级线程(goroutine)、通道(channel)及丰富的同步原语(sync包),使其在CentOS等多平台上能高效处理并发任务。以下是CentOS环境下Golang并发编程的具体实践与关键要点:
在CentOS上使用Golang前,需完成环境配置:
wget https://golang.org/dl/go1.20.4.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.20.4.linux-amd64.tar.gz
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc
go version,确认输出版本信息。Goroutine是Golang并发的基础,使用go关键字启动,由Go运行时管理,内存占用远小于传统线程(约2KB)。示例:启动5个goroutine打印编号:
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
fmt.Printf("Goroutine %d executed\n", id)
}(i)
}
wg.Wait() // 等待所有goroutine完成
}
关键点:通过sync.WaitGroup同步goroutine,避免主程序提前退出。
Channel用于goroutine间的数据传递,避免共享内存带来的竞态条件。示例:通过带缓冲channel实现并发下载任务:
package main
import (
"fmt"
"net/http"
)
func download(url string, ch chan<- string) {
resp, err := http.Get(url)
if err != nil {
ch <- fmt.Sprintf("Error downloading %s: %v", url, err)
return
}
defer resp.Body.Close()
ch <- fmt.Sprintf("Downloaded %s (Status: %s)", url, resp.Status)
}
func main() {
urls := []string{"https://example.com/file1", "https://example.com/file2"}
ch := make(chan string, len(urls)) // 带缓冲channel
for _, url := range urls {
go download(url, ch)
}
for i := 0; i < len(urls); i++ {
fmt.Println(<-ch) // 接收并打印结果
}
}
关键点:带缓冲channel可提高并发效率,避免发送操作阻塞。
package main
import (
"fmt"
"sync"
)
var (
counter int
mu sync.Mutex
)
func increment() {
mu.Lock()
counter++
mu.Unlock()
}
func main() {
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
increment()
}()
}
wg.Wait()
fmt.Println("Final counter:", counter) // 输出1000
}
关键点:读写操作需用Lock()/Unlock()包裹,确保原子性。适用于CPU密集型任务,避免goroutine过多导致资源耗尽。示例:创建3个worker处理5个任务:
package main
import "fmt"
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, j)
results <- j * 2 // 模拟任务处理
}
}
func main() {
jobs := make(chan int, 5)
results := make(chan int, 5)
// 启动3个worker
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
// 发送任务
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// 接收结果
for a := 1; a <= 5; a++ {
fmt.Println("Result:", <-results)
}
}
关键点:通过带缓冲的jobs通道控制任务队列,results通道收集结果。
用于传递取消信号、超时或截止时间,避免goroutine泄漏。示例:使用context取消长时间运行的任务:
package main
import (
"context"
"fmt"
"time"
)
func longTask(ctx context.Context, id int) {
for {
select {
case <-ctx.Done():
fmt.Printf("Task %d cancelled\n", id)
return
default:
fmt.Printf("Task %d running...\n", id)
time.Sleep(500 * time.Millisecond)
}
}
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() // 释放资源
go longTask(ctx, 1)
time.Sleep(3 * time.Second) // 主程序等待
}
关键点:WithTimeout设置2秒超时,超时后ctx.Done()触发,goroutine退出。
-race标志:go run -race main.go
或测试时使用:go test -race ./...
sync/atomic包提升性能:package main
import (
"fmt"
"sync/atomic"
)
func main() {
var counter int32
for i := 0; i < 1000; i++ {
go func() {
atomic.AddInt32(&counter, 1)
}()
}
time.Sleep(time.Second)
fmt.Println("Counter:", atomic.LoadInt32(&counter)) // 输出1000
}
GOMAXPROCS设置并行度(默认为CPU核心数),优化并发性能。