diff options
author | johannst <johannes.stoelp@gmail.com> | 2020-10-05 17:57:26 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2020-10-05 17:57:26 +0200 |
commit | 40335c667870e72deb739b81ffbf8d23902ebe71 (patch) | |
tree | f98587f320ec85f3d912430454e51e1acf99c04f /lib/thread.h | |
parent | e710fae8b8a1c34109644e5991f293b58e7eaa16 (diff) | |
download | matcha-threads-40335c667870e72deb739b81ffbf8d23902ebe71.tar.gz matcha-threads-40335c667870e72deb739b81ffbf8d23902ebe71.zip |
add support for function objects
Diffstat (limited to 'lib/thread.h')
-rw-r--r-- | lib/thread.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/thread.h b/lib/thread.h index d983734..3304892 100644 --- a/lib/thread.h +++ b/lib/thread.h @@ -2,6 +2,9 @@ #pragma once +#include <functional> +#include <memory> + namespace nMatcha { struct Executor; @@ -26,4 +29,24 @@ namespace nMatcha { friend struct Executor; const Executor* mExecutor; }; + + struct Yielder { + virtual void yield() = 0; + }; + + struct FnThread : public Thread, public Yielder { + using UserFn = std::function<void(Yielder&)>; + static std::unique_ptr<Thread> make(UserFn f); + + private: + virtual void threadFn() override; + virtual void yield() override; + + UserFn mUserFn; + + enum class CreatorToken {}; + + public: + FnThread(CreatorToken, UserFn f) : mUserFn(f) {} + }; } // namespace nMatcha |