c++ - How to store parameter pack as members -
so reading thread: c++ how store parameter pack variable
the answer didn't me (i wasn't sure on how implement it)
what i'm trying create thread class has arguments, current way arguments need able stored members, them passed onto member function.
here current thread class: (i want funcargs able stored members, can passed member function in run)
template<class _ty, typename...funcargs> class ythread { private: typedef yvoid(_ty::* ymethod)(); handle _mythread_handle; dword _mythread_id; _ty* _myobject; ymethod _mymethod; private: static yvoid run(ypointer thread_obj) { ythread<_ty>* thread = (ythread<_ty>*)thread_obj; thread->_myobject->*thread->_mymethod(); } ythread(const ythread<_ty>& other) = delete; ythread<_ty>& operator=(const ythread<_ty>& other) = delete; public: // starters ythread() {} ythread(_ty* object, yvoid(_ty::* method)()) { _myobject = object; _mymethod = method; start(); } ~ythread() { if (_mythread_handle) closehandle(_mythread_handle); } ybool start() { _mythread_handle = createthread( 0, 0, (lpthread_start_routine)ythread<_ty>::run, this, 0, &_mythread_id); } ybool start(_ty* object, yvoid(_ty::* method)()) { _myobject = object; _mymethod = method; start(); } public: // other yvoid kill() { terminatethread(_mythread_handle, 0); } yvoid join() { waitforsingleobject(_mythread_handle, infinite); } ybool is_alive() { dword exitcode = 0; if (_mythread_handle) getexitcodethread(_mythread_handle, &exitcode); if (exitcode == still_active) return true; return false; } };
i assume doing educational purposes - because otherwise, suggest stop reinventing wheel , use std::thread
nature intended.
that said - along these lines, perhaps:
class ythread { std::function<void()> thing_to_run; public: template<class ty, typename... funcargs> ythread(ty* object, yvoid(ty::* method)(funcargs...), funcargs&&... args) { thing_to_run = [=](){ (object->*method)(std::forward<funcargs>(args)...); }; } // when comes time run method, // thing_to_run(); };
Comments
Post a Comment