doupaoyan6083 2017-02-03 23:30
浏览 105

为什么未在所有goroutine之间共享ZeroMQ上下文?

In this ZeroMQ example,

// Multithreaded Hello World server.
// Uses Goroutines.  We could also use channels (a native form of
// inproc), but I stuck to the example.
//
// Author:  Brendan Mc.
// Requires: http://github.com/alecthomas/gozmq

package main

import (
    "fmt"
    zmq "github.com/alecthomas/gozmq"
    "time"
)

func main() {
    // Launch pool of worker threads
    for i := 0; i != 5; i = i + 1 {
        go worker()
    }

    // Prepare our context and sockets
    context, _ := zmq.NewContext()
    defer context.Close()

    // Socket to talk to clients
    clients, _ := context.NewSocket(zmq.ROUTER)
    defer clients.Close()
    clients.Bind("tcp://*:5555")

    // Socket to talk to workers
    workers, _ := context.NewSocket(zmq.DEALER)
    defer workers.Close()
    workers.Bind("ipc://workers.ipc")

    // connect work threads to client threads via a queue
    zmq.Device(zmq.QUEUE, clients, workers)

}

func worker() {
    context, _ := zmq.NewContext()
    defer context.Close()

    // Socket to talk to dispatcher
    receiver, _ := context.NewSocket(zmq.REP)
    defer receiver.Close()
    receiver.Connect("ipc://workers.ipc")

    for true {
        received, _ := receiver.Recv(0)
        fmt.Printf("Received request [%s]
", received)

        // Do some 'work'
        time.Sleep(time.Second)

        // Send reply back to client
        receiver.Send([]byte("World"), 0)
    }
}

each goroutine has its own ZeroMQ Context. However, in the ZeroMQ guide, it says the following:

Create one ZeroMQ context at the start of your process, and pass that to all threads that you want to connect via inproc sockets.

Don't share ZeroMQ sockets between threads. ZeroMQ sockets are not threadsafe. Technically it's possible to migrate a socket from one thread to another but it demands skill. The only place where it's remotely sane to share sockets between threads are in language bindings that need to do magic like garbage collection on sockets.

I know that goroutines are not threads.

Instead, they live in threads. But I've also read that it's possible to have a shared object between goroutines.

So, why doesn't the context is shared between goroutines?

I think it'd consume a lot less space, because a context with a socket in it might be kinda big.

But even if it's not that consuming, why at all the context is not shared in this example?

  • 写回答

2条回答 默认 最新

  • dongxinxin7809 2017-02-04 05:30
    关注

    You can absolutely share objects between Goroutines, as long as those objects are threadsafe. The Go ZeroMQ bindings you are using is explicitly non-threadsafe. While a single Goroutine may not correspond to a single OS thread, it is not possible to guarantee that the Goroutines you spawn will live on the same OS thread. Their life cycle is managed dynamically by the Go run-time.

    You can "share" your ZeroMQ socket between multiple Goroutines by using channels to synchronize access, or by using mutexes, where you lock access to your socket while a Goroutine is accessing it.

    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作