Skip to content

Instantly share code, notes, and snippets.

@LuxoftAKutsan
Last active December 29, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuxoftAKutsan/16fc3d16a0d2ca91d9be9689236ea56f to your computer and use it in GitHub Desktop.
Save LuxoftAKutsan/16fc3d16a0d2ca91d9be9689236ea56f to your computer and use it in GitHub Desktop.
template <M>
class AsyncMessageProcessor {
Handle() = 0;
ProcessMessage(M m ) {
// put message in queue
}
Thread tread; // thread pop messages from queue and call Handle for each
void Stop() {
// forbid push
// clear queue ( optional)
thread.join() // process left messages
}
virtual ~AsyncMessageProcessor() {}
};
class XMessagePtocessor : public AsyncMessageProcessor<XMessage> {
void Handle(XMessage m) override {
// do staff
}
~XMessagePtocessor() {
Stop(); // I would like to avoid explicitely call Stop in ~XMessagePtocessor
}
};
// Preferable usage :
{
XMessagePtocessor p;
for (XMessage m: messages) {
p.ProcessMessage(m)
}
} // there code should safely destroy XMessagePtocessor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment