0

I know when I work with sockets or file I can set it for non-block and use epoll for example to make a async server, but my doubt is when I don't have a file descriptor like happens with sockets, for example, imagine a query in a database, how can I make a query without block my thread, and pass a callback like happens on Node.js

for example:

queryDB("select something from mytable", my_callback);

and queryDB return immediately.

My doubt is how do I make this function queryDB with this behavior.

2
  • If the API is only blocking, you can start another thread to do the call. Depending on what the callback must do and in which thread it has to run, things can get tricky though. Commented Apr 18, 2015 at 22:44
  • In fact I would like to avoid threads, I would like to do something like happen with read function for example, I would like to deliver the task to O.S and after gets the response by epoll. Commented Apr 18, 2015 at 22:49

1 Answer 1

1

Synchronous interfaces cannot be used asynchronously without introducing a separate call stack. Btw, OS is your I/O thread.

Start a single thread, pop queries from atomic queue, signal their completion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.