0

This is some code i'm using in Java for Making Asynchronous function calls in Java :

    public class AsyncLogger
    {
        public static asyncLog = null;
        public static ExecutorService executorService = Executors.newSingleThreadExecutor();

        public static AsyncLogger GetAsyncClass()
        {
            if(asyncLog == null)
            {
                asyncLog= new AsyncLogger();
            }
            return asyncLog;
        }


        public void WriteLog(String logMesg)
        {
            executorService.execute(new Runnable()
            {
                public void run()
                {
                    WriteLogDB(logMesg);
                }
            });
                }

                public void ShutDownAsync()
                {
                    executorService.shutdown();
        }
    }

This is a Singleton Class with static ExecutorService and WriteLogDB will be called as an Asynchronous function. So i can process my Code in WriteLogDB asynchronously without affecting the main flow.

Can i get a C++ equivalent like this ..?

Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62

2 Answers2

6

You can make asynchronous functions calls using std::async from C++11.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • ...or alternatively if you don't have c++11, but do have boost, consider my answer here: http://stackoverflow.com/questions/4084777/creating-a-thread-pool-using-boost/4085345#4085345 – Nim Sep 25 '12 at 12:47
6
std::thread([](){WriteLogDB(logMesg);}).detach();

or if you need to wait for a result:

auto result = std::async(std::launch::async, [](){WriteLogDB(logMesg);});
// do stuff while that's happening
result.get();

If you're stuck with a pre-2011 compiler, then there are no standard thread facilities; you'll need to use a third-party library like Boost, or roll you own, platform specific, threading code. Boost has a thread class similar to the new standard class:

boost::thread(boost::bind(WriteLogDB, logMesg)).detach();
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Also known as `fire_and_forget([]{ WriteLogDB(logMsg); });`. :) E: Why did you change the lambda to `bind`? – Xeo Sep 25 '12 at 12:52
  • @ManikandarajS: The first version certainly will. The second is implementation-defined; it might create a thread, or it might use a thread pool or something. – Mike Seymour Sep 25 '12 at 12:55
  • @Xeo: Because I'm more comfortable with `bind`. I'll put the lambda back as an alternative for those who like squiggles. – Mike Seymour Sep 25 '12 at 12:57
  • And for those who like performance, `bind` isn't the fastest thing on earth. :) – Xeo Sep 25 '12 at 13:00
  • @Mike I don't want to create separate Threads for each function call, that would be difficult. I can run 3 or 4 threads in a thread Pool and try the Async call – Manikandaraj Srinivasan Sep 25 '12 at 13:03
  • @Xeo: I guess so; presumably, `bind` will use a function pointer, so is probably less inlineable. – Mike Seymour Sep 25 '12 at 13:07
  • @ManikandarajS: That sounds rather more difficult than just using the standard library facilities; but if implementing a thread pool sounds like fun, then go ahead. – Mike Seymour Sep 25 '12 at 13:09
  • @Mike I just want to keep the number of threads limited say 2 or 3. But the function calls may be more. ExecutorService in java service can be limited to a Single Thread and it can run Asynchronous functions in single thread from its own memory queue – Manikandaraj Srinivasan Sep 25 '12 at 14:33