A Server which will run forever and processes requests needs an asynchronous part of code in it which will execute some database queries and update only when there are any new changes to it. The server has to run forever and this function for executing a db function again and again has to run asynchronously, so that there is not hindrance the server because of update once in every ‘x’ minutes.
How best this can be processed asynchronously in c++ ? How can I set that function alone to run on a daemon so that it doesn’t block the server at all ?
I’d strongly recommend using Boost’s ASIO library
You’d need a class to accept new requests and another to periodically check for updates. Both could do their work asynchronously and use the same boost::asio::io_service to schedule work.
The setup would be
boost::asio::ip::tcp::acceptorlistening for new requests.boost::asio::deadline_timedo an asynchronous wait do check for updates to the database.Pseudo code for what I understand you are describing is below:
Compiling the above and running it will show that the Database Update Checker will check for updates every 5 seconds while listening for connections on TCP port 4444. To see the code accept a new connection you can use telnet/netcat/your favorite network client tool….
If you find that the processing of updates and/or requests takes a significant amount of time then I’d look into threading your application and running each task in it’s own thread. io_service will schedule what work it has to do and not complete until there is no more work. The trick is to have the classes doing work reschedule themselves when they are done.
Of course you have to take into account the comments of others on your question. I dont’ know how a CORBA interface might complicate this but I would think boost::asio as an asynchronous C++ library would be a good decision and flexible enough for what you describe.