1

Perhaps the topic of std::bind and std::function is not clear enough to me - then, please, clarify. I'm trying to implement a thread pool (like this), but to avoid std::function and std::bind performance penalty due to heap allocations for the asynchronous function parameters stored somewhere in std::function or std::bind. Here they explain how to provide a custom allocator to std::function. However, as I understand, custom allocator (for performance) should be provided to std::bind too. If not, then why? If yes, how?

EDIT1: if that matters, the custom allocator is going to allocate from a memory pool.

EDIT2: the compiler is MSVC++2017, so C++11/14/17 should (partially) work.

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • You don't need std::bind at all, use lambda. –  Jul 15 '17 at 18:16
  • @manni66 There is at least one use for `std::bind` which can't be copied with lambdas: [pre-C++14 move-capture](https://stackoverflow.com/a/12744730/2752075). – HolyBlackCat Jul 15 '17 at 18:23
  • @manni66, as I understand, lambda will still be a class that will store the parameters in the heap. So the question just changes to "how to specify custom allocator for lambdas" in that case. – Serge Rogatch Jul 15 '17 at 19:51
  • I don't believe a lambda requires any heap allocations, except to the extent that objects it captures by value allocate in their copy constructors. In fact, it could be argued that a lambda shall not perform heap allocations. The standard carefully spells out exactly what the class behind a lambda should look like; captures become data members of that class. – Igor Tandetnik Jul 15 '17 at 20:19
  • A lambda is essentially a struct with operator(). It has no memory management. –  Jul 16 '17 at 08:23

0 Answers0