[nanomsg] Re: Getting rid of nn_init() and nn_term()

  • From: Martin Sustrik <sustrik@xxxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Mon, 04 Feb 2013 09:16:55 +0100

Hi Paul,

I find the following two situations subtle:

1. It is unsafe to create two sockets in two different threads, unless
you already created at least one socket. Am I right?

I've already solved this problem. The global state is guarded by a static mutex, thus only the first call to nn_socket creates the global state. The second call just re-uses the existing state.

2. When you close and reopen socket, sometimes worker threads are shut
down and started again. Sometimes not.

Closing and opening sockets is useful for some applications. While I
don't have good use cases that closes sockets many times per second, I
might be not very creative :)

Yes. there would be some performance impact. The question is whether this rare use case (opening and closing one socket in a tight loop, combined with the need for extremely high performance) is worth of making the API more complex. In other words, 999 developers out of 1000 would pay the price of added complexity for the one that actually needs this optimisation.

Btw, if keeping worker threads alive is important, the user can still open one dummy socket (not bound or connected) to ensure that.

Martin

Other related posts: