Sane C++ Libraries: SC::AsyncLoopWork Struct Reference (original) (raw)
Executes work in a thread pool and then invokes a callback on the event loop thread. More...
Executes work in a thread pool and then invokes a callback on the event loop thread.
AsyncLoopWork::work is invoked on one of the thread supplied by the ThreadPool passed during AsyncLoopWork::start. AsyncLoopWork::callback will be called as a completion, on the event loop thread AFTER work callback is finished.
static constexpr int NUM_THREADS = 4;
static constexpr int NUM_WORKS = NUM_THREADS * NUM_THREADS;
ThreadPool threadPool;
AsyncEventLoop eventLoop;
AsyncLoopWork works[NUM_WORKS];
int numAfterWorkCallbackCalls = 0;
Atomic numWorkCallbackCalls = 0;
for (int idx = 0; idx < NUM_WORKS; ++idx)
{
works[idx].work = [&]
{
Thread::Sleep(50);
numWorkCallbackCalls.fetch_add(1);
return Result(true);
};
works[idx].callback = [&](AsyncLoopWork::Result&)
{
numAfterWorkCallbackCalls++;
};
}
int numRequests = 0;
eventLoop.enumerateRequests([&](AsyncRequest&) { numRequests++; });
SC_TEST_EXPECT(numWorkCallbackCalls.load() == NUM_WORKS);