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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:44:01+00:00 2026-05-27T13:44:01+00:00

A Server which will run forever and processes requests needs an asynchronous part of

  • 0

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 ?

  • 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-27T13:44:02+00:00Added an answer on May 27, 2026 at 1:44 pm

    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

    • A network asynchronous boost::asio::ip::tcp::acceptor listening for new requests.
    • A boost::asio::deadline_time do an asynchronous wait do check for updates to the database.

    Pseudo code for what I understand you are describing is below:

    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/bind.hpp>
    #include <boost/shared_ptr.hpp>
    #include <string>
    
    class DatabaseUpdateChecker{
        public:
        DatabaseUpdateChecker(boost::asio::io_service& io, const int& sleepTimeSeconds)
        :timer_(io,boost::posix_time::seconds(sleepTimeSeconds)),sleepSeconds_(sleepTimeSeconds){
            this->timer_.async_wait(boost::bind(&DatabaseUpdateChecker::doDBUpdateCheck,this,boost::asio::placeholders::error));
        };
    
        protected:
        void doDBUpdateCheck(const boost::system::error_code& error){
            if(!error){
                std::cout << " Checking Database for updates" << std::endl;
                //Reschdule ourself
                this->timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(this->sleepSeconds_));
                this->timer_.async_wait(boost::bind(&DatabaseUpdateChecker::doDBUpdateCheck,this,boost::asio::placeholders::error));
            }
        };
        private:
        boost::asio::deadline_timer timer_;
        int sleepSeconds_;  
    };
    
    typedef boost::shared_ptr<boost::asio::ip::tcp::socket> TcpSocketPtr;
    
    class NetworkRequest{
        public: 
        NetworkRequest(boost::asio::io_service& io, const int& port)
        :acceptor_(io,boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),port)){
            this->start_accept();   
        };  
        protected:
        void start_accept(){
            TcpSocketPtr socketPtr(new boost::asio::ip::tcp::socket(acceptor_.get_io_service()));
            std::cout << "About to accept new connection" << std::endl;
            acceptor_.async_accept(*socketPtr,boost::bind(&NetworkRequest::handle_accept,this,socketPtr,boost::asio::placeholders::error));
        };  
        void handle_accept(TcpSocketPtr socketPtr,const boost::system::error_code& error){
            std::cout << "Accepted new network connection" << std::endl;
            if(!error){
                std::string response("This is a response\n");
                boost::asio::async_write(*socketPtr,boost::asio::buffer(response),
                    boost::bind(&NetworkRequest::handle_write,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
            }
            //Start listeing for a new connection
            this->start_accept();
        }   
        void handle_write(const boost::system::error_code& error,size_t size){
            if(!error){
                std::cout << "Wrote out " << size << " bytes to the network connection" << std::endl;
            }
    
        }   
        private:
        boost::asio::ip::tcp::acceptor acceptor_;
    };
    
    int main(int argc, char *argv[]) {
        static const int DB_TIMER_SECONDS=5;
        static const int LISTENING_TCP_PORT=4444;
    
        std::cout << "About to start" << std::endl;
        boost::asio::io_service io;
    
        DatabaseUpdateChecker dbChecker(io,DB_TIMER_SECONDS);
        NetworkRequest networkRequestAcceptor(io,LISTENING_TCP_PORT);
    
        io.run();
    
        std::cout << "This won't be printed" << std::endl;  
        return 0;
    }
    

    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….

    telnet 127.0.0.1 4444
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    This is a response
    Connection closed by foreign host.
    

    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.

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

Sidebar

Related Questions

I have within my Sql Server 2008 database a trigger which will run on
When I develop a C# console application (which will run on a server) and
Is that possible to have a single PHP SOAP server which will handle requests
I've written a simple server application which will run distributed on several machines. My
i need to write a socket server using perl which will run on a
I've recently started on a project to develop some portlets which will run on
I am developing PDF to e-book converter which will run on a server. I
I am needing to write portable code that will run on a shared server
I have a Rails server which will need to run a python script at
I'm writing an ASP.NET web application which will run on Windows Server 2008 (IIS7).

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.