相关疑难解决方法(0)

Golang,如何分享价值 - 消息或互斥?

我已经完成了简单的基准测试,其中一个在消息传递和锁定共享值方面更有效.

首先,请检查下面的代码.

package main

import (
    "flag"
    "fmt"
    "math/rand"
    "runtime"
    "sync"
    "time"
)

type Request struct {
    Id      int
    ResChan chan Response
}

type Response struct {
    Id    int
    Value int
}

func main() {
    procNum := flag.Int("proc", 1, "Number of processes to use")
    clientNum := flag.Int("client", 1, "Number of clients")
    mode := flag.String("mode", "message", "message or mutex")
    flag.Parse()

    if *procNum > runtime.NumCPU() {
        *procNum = runtime.NumCPU()
    }

    fmt.Println("proc:", *procNum)
    fmt.Println("client:", *clientNum)
    fmt.Println("mode:", *mode)

    runtime.GOMAXPROCS(*procNum)

    rand.Seed(time.Now().UnixNano())
    var wg sync.WaitGroup

    sharedValue …
Run Code Online (Sandbox Code Playgroud)

mutex channel go

0
推荐指数
1
解决办法
3196
查看次数

标签 统计

channel ×1

go ×1

mutex ×1