0

I am attempting to write a work queue, however I don't really want to know the argument types until later, anyways I am attempting to write something to the effect of


std::queue<std::packaged_task<void()>> workQueue{};

template <typename... Args>
void Foo::doWork(char const* key, Args&&... args)
{
    // this already works if called directly but would like to do this work from a separate thread pulling from that queue
}

template <typename... Args>
void Foo::enqueue(char const* key, Args&&... args)
{
    // Cannot seem to push this work into a queue type error occurs
    workQueue.emplace(std::bind(&Foo::doWork<Args&&...>, this, key, args...));
}

example usage

foo.enqueue("bar", 3, 4, "wham", true);

I am trying to queue up this work as fast as reasonable to keep as much work off the gathering side of this queue. I would simply serialize arguments into something like a stringstream but then I would have to do a sort of cast back to type action on the deserialize side, worry about message + boundaries etc and I would like to avoid that. Anyone got ideas what I could be doing wrong here to get type errors, also attempting to avoid boost so stl containers would be best. I am not able to know the structure ahead of time to just specialize this. Also u want to make sure the arguments are copied into the queue rather then simply referenced so that they are useful even after being de-allocated from the stack off the calling function.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Are you able to use C++17, or are you bound to C++14? – Nikos C. Jun 14 '19 at 02:07
  • Pretty bound to c++14 but i may also be able to work it backwards a bit if there was a c++17 example. I am not trying to allocate std::variants to hold these in an attempt to reduce the allocations taken on the enqueue side. – David Wasser Jun 14 '19 at 02:20
  • i guess really if i cared about allocations then maybe a stringstream is the way to go but would really want to try this first, really doing whatever i can eek out performance on the gathering side even if its to the determent of pulling out side. – David Wasser Jun 14 '19 at 02:28
  • `'bar'` and `'wham'` are not string literals but multi-char literals; their type is `int`, and you are passing one where `const char*` is expected. If that's your actual code, that's likely where your problem lies. If this is not your actual code, then show a [mcve] and the exact and complete text of any error messages. – Igor Tandetnik Jun 14 '19 at 04:20
  • Here https://ideone.com/eKhmeY actually removed the queue error is around the bind error of the form `error: no match for call to ‘(std::_Bind(Foo*, const char*, const char*, const char*)>) ()’ boundF();` – David Wasser Jun 14 '19 at 13:20

0 Answers0