Search code examples
multithreadingc++11boost-threadstdthread

Reducing the number of heap allocations when enqueuing tasks


Heap allocations are a bottleneck in my application and I would like to avoid them when sending small tasks to my thread pool.

Can I use a std::packaged_task with a stack allocator? Under which conditions? What are the pros/cons of this choice? Are there better alternatives to avoid heap allocations of std::future's shared state by operator new ?

auto foo() {
  arena<1024> buffer;
  auto task = std::packaged_task<int()>{
    std::allocator_arg_t, 
    arena_allocator{arena},
    []() -> int { return 5; } 
  };
  auto f = task.get_future();  // is this future and its shared state stack allocated?
  thread_pool.push_back(std::move(task)); 
  // I will probably need to block before the stack goes out of scope..
  return f.get();
}

Solution

  • Your "I will probably need to block before the stack goes out of scope" comment clearly identifies the only issue here. The only thing you must make sure is that because the task in your sending thread's stack, it has to stay there until your thread pool executes it.

    Other than that, there are no issues with using the stack, instead of heap allocation.