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 206427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:38:43+00:00 2026-05-11T17:38:43+00:00

Whilst asynchronous IO (non-blocking descriptors with select/poll/epoll/kqueue etc) is not the most documented thing

  • 0

Whilst asynchronous IO (non-blocking descriptors with select/poll/epoll/kqueue etc) is not the most documented thing on the web, there are a handful of good examples.

However, all these examples, having determined the handles that are returned by the call, just have a ‘do_some_io(fd)‘ stub. They don’t really explain how to best approach the actual asynchronous IO in such a method.

Blocking IO is very tidy and straightforward to read code. Non-blocking, async IO is, on the other hand, hairy and messy.

What approaches are there? What are robust and readable?

void do_some_io(int fd) {
  switch(state) {
    case STEP1:
       ... async calls
       if(io_would_block)
          return;
       state = STEP2;
    case STEP2:
       ... more async calls
       if(io_would_block)
          return;
       state = STEP3;
    case STEP3:
       ...
  }
}

or perhaps (ab)using GCC’s computed gotos:

#define concatentate(x,y) x##y
#define async_read_xx(var,bytes,line)       \
   concatentate(jmp,line):                  \
   if(!do_async_read(bytes,&var)) {         \
       schedule(EPOLLIN);                   \
       jmp_read = &&concatentate(jmp,line); \
       return;                              \
}

// macros for making async code read like sync code
#define async_read(var,bytes) \
    async_read_xx(var,bytes,__LINE__)

#define async_resume()            \
     if(jmp_read) {               \
         void* target = jmp_read; \
         jmp_read = NULL;         \
         goto *target;            \
     }

void do_some_io() {
   async_resume();
   async_read(something,sizeof(something));
   async_read(something_else,sizeof(something_else));
}

Or perhaps C++ exceptions and a state machine, so worker functions can trigger the abort/resume bit, or perhaps a table-driven state-machine?

Its not how to make it work, its how to make it maintainable that I’m chasing!

  • 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-05-11T17:38:43+00:00Added an answer on May 11, 2026 at 5:38 pm

    I suggest take a look on: http://www.kegel.com/c10k.html, second take a look on existing libraries like libevent, Boost.Asio that already do the job and see how they work.

    The point is that the approach may be different for each type of system call:

    • select is simple reactor
    • epoll have both edge or level triggered interface that require different approach
    • iocp is proactor require other approach

    Suggestion: use good existing library like Boost.Asio for C++ or libevent for C.

    EDIT: This is how ASIO handles this

    class connection {
       boost::asio:ip::tcp::socket socket_;
    public:
       void run()
       {
             // for variable length chunks
             async_read_until(socket_,resizable_buffer,'\n',
                   boost::bind(&run::on_line_recieved,this,errorplacehplder);
             // or constant length chunks
             async_read(socket_,buffer(some_buf,buf_size),
                   boost::bind(&run::on_line_recieved,this,errorplacehplder);
       }
       void on_line_recieved(error e)
       {
            // handle it
            run();
       }
    
    };
    

    Because ASIO works as proactor it notifies you when operation is complete and
    handles EWOULDBLOCK internally.

    If you word as reactor you may simulate this behavior:

     class conn {
        // Application logic
    
        void run() {
           read_chunk(&conn::on_chunk_read,size);
        }
        void on_chunk_read() {
             /* do something;*/
        }
    
        // Proactor wrappers
    
        void read_chunk(void (conn::*callback),int size, int start_point=0) {
           read(socket,buffer+start,size)
           if( complete )
              (this->*callback()
           else {
              this -> tmp_size-=size-read;
              this -> tmp_start=start+read;
              this -> tmp_callback=callback
              your_event_library_register_op_on_readable(callback,socket,this);
           }
        }
        void callback()
        {
           read_chunk(tmp_callback,tmp_size,tmp_start);
        }
     }
    

    Something like that.

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

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no BlackBerry 9300 model as far as I… May 12, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer If you created the elements using document.createElement() (but didn't append… May 12, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer You could do it using Routes, i.e. routes.MapAreaRoute("Blogs", "Blog/Views/{controller}/{action}/{id}", new… May 12, 2026 at 9:47 pm

Related Questions

Im in the process of writing a python script to act as a glue
I have a UINavigationController onto which I push a 'loading screen' UIViewController whilst I
We have a SQL 2005 database server which uses high safety (synchronous, no automatic
Whilst analysing some legacy code with FXCop, it occurred to me is it really

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.