From 40335c667870e72deb739b81ffbf8d23902ebe71 Mon Sep 17 00:00:00 2001 From: johannst Date: Mon, 5 Oct 2020 17:57:26 +0200 Subject: add support for function objects --- example/demo1.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'example') diff --git a/example/demo1.cc b/example/demo1.cc index 1c910cd..b83c434 100644 --- a/example/demo1.cc +++ b/example/demo1.cc @@ -10,15 +10,21 @@ struct TestThread : public nMatcha::Thread { TestThread(const char* name) : Thread(), mName(name) {} virtual void threadFn() override { - printf("[%s] starting up TestThread -> yield()\n", mName); + printf("[TestThread(%s)] start -> yield\n", mName); yield(); - printf("[%s] yield() -> finishing TestThreads\n", mName); + printf("[TestThread(%s)] yield -> done\n", mName); } private: const char* mName; }; +void freeThreadFn(nMatcha::Yielder& y) { + puts("[freeThreadFn] start -> yield"); + y.yield(); + puts("[freeThreadFn] yield -> done"); +} + int main() { puts("[main] start main thread"); @@ -26,6 +32,15 @@ int main() { e.spawn(std::make_unique("Thread1")); e.spawn(std::make_unique("Thread2")); e.spawn(std::make_unique("Thread3")); + + e.spawn(nMatcha::FnThread::make([](nMatcha::Yielder& y) { + puts("[Lambda Thread] start -> yield"); + y.yield(); + puts("[Lambda Thread] yield -> done"); + })); + + e.spawn(nMatcha::FnThread::make(freeThreadFn)); + e.run(); puts("[main] finish main thread"); -- cgit v1.2.3