Thread: non blocking io

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    20

    Question non blocking io

    Hi everyone!
    I am making a scgi library(yeah, I know that there lots of them already), and I have a problem. I am using libev for event based system with non blocking synchronous io. I need to manage somehow to make it possible for user to do non blocking io as well. I have been considering two approaches. The first one is to use contexts, but they are non standart and theoretically very memory consuming(it will have to copy the whole stack, which is by default about several mbs compared to my less-than-20-kb-per-connection-schema, right?). The second one is saving pointer to function which will be executed on readines of the io, save data in user defined structure pointed to by special "void *user_data" field, and wait for another event, upon which, execute specified "next function". But this is complex and inconvinient. What would you suggest?
    Last edited by wirmius; 05-09-2013 at 09:15 AM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    What operating system will your scgi library be running on?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    The second one is saving pointer to function which will be executed on readines of the io, save data in user defined structure pointed to by special "void *user_data" field, and wait for another event, upon which, execute specified "next function". But this is complex and inconvinient. What would you suggest?
    this is similar to how node.js works (which uses libuv, which is like libev but is portable to linux and windows). seems complex at first to those used to linear programs, but once figured out, becomes effective.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    After reading more about libev, seems like pointer to function would be the way to go. When the user's function is called, is there some synchronization method that would allow the function to set an event that the main user code could be pending on? In other situations, sometimes instead of using non-blocking io, multi-threading is used instead with some threads dedicated for blocked io operations (while the other threads continue to run), and then using the normal synchronization methods like mutexes or semaphores. Is this not a reasonable approach for your particular application?

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    But this is complex and inconvinient. What would you suggest?
    O_o

    Actually, I find that iterating over "ready", "fill", and "next" events, or similar, is one of the few ways correctly exposing asynchronous facilities.

    As dmh2000 says, it may seem complex at first, but after the client understands how to handle the cycle it is very natural.

    Is this not a reasonable approach for your particular application?
    O_o

    Well, though it depends on the underlying operating system, using asynchronous events is generally faster than threading approaches.

    Take a look at your suggest; you are basically building a synchronous mechanism using several different events. If you can use the asynchronous primitives directly, you will not need to pay the price of those synchronization primitives.

    Soma

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    20
    The target OS is linux of course.
    The main downside in convinience I see in next function approach - good bye stack variables.
    I would use the threaded method, if I didn't have goal of making it purely event driven.
    Is there really no other options?

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by wirmius View Post
    ... linux ... I would use the threaded method, if I didn't have goal of making it purely event driven.
    Assuming this is a preemptive version of linux, then why wouldn't you consider multi-threading to be event driven? If a higher priority thread is pending on an event signalled by a thread performing the blocked io, then a context switch will occur as soon as the event is signalled. Isn't that good enough?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with non-blocking IO
    By soundstep in forum C Programming
    Replies: 4
    Last Post: 01-12-2011, 07:31 AM
  2. Non Blocking I/O
    By PetrolMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2009, 08:47 PM
  3. Pros and Cons of blocking versus non-blocking sockets?
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-08-2008, 06:52 AM
  4. Non-Blocking Sockets
    By unkownname in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-30-2006, 09:33 PM
  5. Non-Blocking IO
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2002, 04:45 PM

Tags for this Thread