diff --git a/main.cpp b/main.cpp index f952d82c..ff3874eb 100644 --- a/main.cpp +++ b/main.cpp @@ -5,11 +5,9 @@ void test() { } int main() { - PITimer t([](){piCout << "timer";}); -// t.setSlot(test); - t.addDelimiter(5, [](void * d){piCout << "delim 5";}); - t.addDelimiter(2, [](){piCout << "delim 2";}); - t.start(100); - piMSleep(2000); + PIThread::runOnce([](){ while(1) piCout << "thread1"; }); + PIThread::runOnce([](){ while(1) piCout << "thread2"; }); + PIThread::runOnce([](){ while(1) piCout << "thread3"; }); + piMSleep(1); return 0; } diff --git a/src_main/concurrent/piconditionvar.h b/src_main/concurrent/piconditionvar.h index 08767eb1..99886b6c 100644 --- a/src_main/concurrent/piconditionvar.h +++ b/src_main/concurrent/piconditionvar.h @@ -6,7 +6,7 @@ #define PIP_TESTS_PICONDITIONVAR_H #include "piconditionlock.h" -#include +#include "pithread.h" #include "piinit.h" /** @@ -101,9 +101,8 @@ private: }; -class PIThread; -typedef void (*ThreadFunc)(void * ); +// FIXME: remove that! class StdFunctionThreadFuncAdapter { public: static void threadFuncStdFunctionAdapter(void* it); @@ -112,7 +111,7 @@ public: void registerToInvoke(PIThread* thread); void* data() const { return (void*)this; } - ThreadFunc threadFunc() const { return threadFuncStdFunctionAdapter; } + ThreadFunc threadFunc() const { return threadFuncStdFunctionAdapter; } private: std::function fun; }; diff --git a/src_main/console/pikbdlistener.h b/src_main/console/pikbdlistener.h index da5119b9..56adf6ca 100644 --- a/src_main/console/pikbdlistener.h +++ b/src_main/console/pikbdlistener.h @@ -133,8 +133,11 @@ public: bool direction; }; +#ifdef PIP_CXX11_SUPPORT + typedef std::function KBFunc; +#else typedef void (*KBFunc)(KeyEvent, void * ); - +#endif //! Constructs keyboard listener with external function "slot" and custom data "data" explicit PIKbdListener(KBFunc slot = 0, void * data = 0, bool startNow = true); @@ -151,6 +154,11 @@ public: //! Set external function to "slot" void setSlot(KBFunc slot) {ret_func = slot;} +#ifdef PIP_CXX11_SUPPORT + //! Set external function to "slot" + void setSlot(std::function slot) {ret_func = [slot](KeyEvent e, void *){slot(e);};} +#endif + //! Returns if exit key if awaiting bool exitCaptured() const {return exit_enabled;} diff --git a/src_main/thread/pithread.cpp b/src_main/thread/pithread.cpp index bf2434fa..f312397b 100755 --- a/src_main/thread/pithread.cpp +++ b/src_main/thread/pithread.cpp @@ -172,6 +172,21 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay) } +#ifdef PIP_CXX11_SUPPORT +PIThread::PIThread(std::function func, bool startNow, int timer_delay) { + PIINTROSPECTION_THREAD_NEW(this); + tid_ = -1; + PRIVATE->thread = 0; + data_ = 0; + ret_func = [func](void*){func();}; + terminating = running_ = lockRun = false; + priority_ = piNormal; + delay_ = timer_delay; + if (startNow) start(timer_delay); +} +#endif + + PIThread::PIThread(bool startNow, int timer_delay): PIObject() { PIINTROSPECTION_THREAD_NEW(this); tid_ = -1; @@ -533,3 +548,11 @@ void PIThread::__thread_func_once__() { _endThread(); } + +PIThread * PIThread::runOnce(std::function func) { + PIThread * t = new PIThread(); + t->setSlot(func); + t->startOnce(); + return t; +} + diff --git a/src_main/thread/pithread.h b/src_main/thread/pithread.h index 1c482849..5c0d9212 100755 --- a/src_main/thread/pithread.h +++ b/src_main/thread/pithread.h @@ -57,7 +57,11 @@ public: static __PIThreadCollection_Initializer__ __PIThreadCollection_initializer__; +#ifdef PIP_CXX11_SUPPORT +typedef std::function ThreadFunc; +#else typedef void (*ThreadFunc)(void * ); +#endif class PIP_EXPORT PIThread: public PIObject { @@ -67,7 +71,12 @@ public: //! Contructs thread with custom data "data", external function "func" and main loop delay "loop_delay". PIThread(void * data, ThreadFunc func, bool startNow = false, int loop_delay = -1); - + +#ifdef PIP_CXX11_SUPPORT + //! Contructs thread with external function "func" and main loop delay "loop_delay". + PIThread(std::function func, bool startNow = false, int loop_delay = -1); +#endif + //! Contructs thread with main loop delay "loop_delay". PIThread(bool startNow = false, int loop_delay = -1); @@ -86,8 +95,15 @@ public: EVENT_HANDLER1(bool, start, int, timer_delay); bool start(ThreadFunc func) {return start(func, -1);} bool start(ThreadFunc func, int timer_delay) {ret_func = func; return start(timer_delay);} +#ifdef PIP_CXX11_SUPPORT + bool start(std::function func) {return start(func, -1);} + bool start(std::function func, int timer_delay) {ret_func = [func](void*){func();}; return start(timer_delay);} +#endif EVENT_HANDLER0(bool, startOnce); EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();} +//#ifdef PIP_CXX11_SUPPORT +// EVENT_HANDLER1(bool, startOnce, std::function, func) {ret_func = [func](void*){func();}; return startOnce();} +//#endif EVENT_HANDLER0(void, stop) {stop(false);} EVENT_HANDLER1(void, stop, bool, wait); EVENT_HANDLER0(void, terminate); @@ -98,6 +114,11 @@ public: //! \brief Set external function that will be executed after every \a run() void setSlot(ThreadFunc func) {ret_func = func;} +#ifdef PIP_CXX11_SUPPORT + //! \brief Set external function that will be executed after every \a run() + void setSlot(std::function func) {ret_func = [func](void*){func();};} +#endif + //! \brief Set priority of thread void setPriority(PIThread::Priority prior); @@ -134,6 +155,10 @@ public: EVENT(started) EVENT(stopped) +#ifdef PIP_CXX11_SUPPORT + static PIThread * runOnce(std::function func); +#endif + //! \handlers //! \{