0

これは、Java で非同期関数呼び出しを行うために 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();
        }
    }

これは静的な ExecutorService を持つシングルトン クラスであり、WriteLogDB は非同期関数として呼び出されます。したがって、メイン フローに影響を与えることなく、WriteLogDB のコードを非同期的に処理できます。

このようなC++の同等物を入手できますか..?

4

2 に答える 2

6

C++11からstd::asyncを使用して、非同期関数呼び出しを行うことができます。

于 2012-09-25T12:44:04.290 に答える
5
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();
于 2012-09-25T12:50:18.620 に答える