Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:43:31+00:00 2026-06-07T06:43:31+00:00

I know I can wait on multiple channels using the select {} syntax in

  • 0

I know I can wait on multiple channels using the select {} syntax in Go, and wait on multiple file descriptors using syscall.Select() or similar functions. But is it possible to wait on both channels at once?

For background, I want to have a goroutine that accepts for messages over a channel and forwards them over a socket connection (provided by gozmq), while simultaneously waiting for replies on the socket connection.

Due to the thread safety requirements of the underlying library, the socket can only be accessed in one thread at a time, which is why I was wondering if there is a way to handle this from a single goroutine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:43:33+00:00Added an answer on June 7, 2026 at 6:43 am

    Selecting on both a channel and an file descriptor is not possible because the abstractions are at different levels. Channels are handled by the go runtime and file descriptors by the operating system. What you need is to make a bridge between them and this can be done with a net.Pipe().

    Pretty much what you need to do is dedicate one goroutine to epoll()/select() to watch your zmq-sockets and a single “wake up” net.Pipe(). This is your poll server. Another goroutine listens on your read and write channels. When someone sends on the read or write channels, the second goroutine would send on the pipe to wake up the poll server.

    This is how the net package in the standard library works. I highly recommend reading it for inspiration (or stealing… the BSD license is very liberal).

    Here is a description of pollServer from net itself. You may need to read the code to understand what this is saying, but this section from fd.go should be a good place to start looking.

    // A pollServer helps FDs determine when to retry a non-blocking
    // read or write after they get EAGAIN.  When an FD needs to wait,
    // send the fd on s.cr (for a read) or s.cw (for a write) to pass the
    // request to the poll server.  Then receive on fd.cr/fd.cw.
    // When the pollServer finds that i/o on FD should be possible
    // again, it will send fd on fd.cr/fd.cw to wake any waiting processes.
    // This protocol is implemented as s.WaitRead() and s.WaitWrite().
    //
    // There is one subtlety: when sending on s.cr/s.cw, the
    // poll server is probably in a system call, waiting for an fd
    // to become ready.  It's not looking at the request channels.
    // To resolve this, the poll server waits not just on the FDs it has
    // been given but also its own pipe.  After sending on the
    // buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a
    // byte to the pipe, causing the pollServer's poll system call to
    // return.  In response to the pipe being readable, the pollServer
    // re-polls its request channels.
    //
    // Note that the ordering is "send request" and then "wake up server".
    // If the operations were reversed, there would be a race: the poll
    // server might wake up and look at the request channel, see that it
    // was empty, and go back to sleep, all before the requester managed
    // to send the request.  Because the send must complete before the wakeup,
    // the request channel must be buffered.  A buffer of size 1 is sufficient
    // for any request load.  If many processes are trying to submit requests,
    // one will succeed, the pollServer will read the request, and then the
    // channel will be empty for the next process's request.  A larger buffer
    // might help batch requests.
    //
    // To avoid races in closing, all fd operations are locked and
    // refcounted. when netFD.Close() is called, it calls syscall.Shutdown
    // and sets a closing flag. Only when the last reference is removed
    // will the fd be closed.
    

    Good luck re-implementing net. The good news at the end of all this your zmq-socket will be thread safe in go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that I know can be done using a subselect, but
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know you can not set a key value dynamically, but what about the
I know you can share control easily with master page. But what if I
Is it possible to have multiple sockets, which can either by TCP or UDP
I am trying to create multiple processes using fork() and execvp() calls, but so
Please let me know how I can print After wait; how can I notify
I have code (my c++ socket server) but I don't know How can I
I have a thread pool created using java.util.concurrent.ThreadPoolExecutor Is there anyway I can wait
I know there are multiple plugins out there however I wonder how I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.