PIThread cxx11 support

git-svn-id: svn://db.shs.com.ru/pip@883 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2020-02-25 15:38:35 +00:00
parent ddd06de776
commit f8f627360a
5 changed files with 65 additions and 12 deletions

View File

@@ -5,11 +5,9 @@ void test() {
} }
int main() { int main() {
PITimer t([](){piCout << "timer";}); PIThread::runOnce([](){ while(1) piCout << "thread1"; });
// t.setSlot(test); PIThread::runOnce([](){ while(1) piCout << "thread2"; });
t.addDelimiter(5, [](void * d){piCout << "delim 5";}); PIThread::runOnce([](){ while(1) piCout << "thread3"; });
t.addDelimiter(2, [](){piCout << "delim 2";}); piMSleep(1);
t.start(100);
piMSleep(2000);
return 0; return 0;
} }

View File

@@ -6,7 +6,7 @@
#define PIP_TESTS_PICONDITIONVAR_H #define PIP_TESTS_PICONDITIONVAR_H
#include "piconditionlock.h" #include "piconditionlock.h"
#include <functional> #include "pithread.h"
#include "piinit.h" #include "piinit.h"
/** /**
@@ -101,9 +101,8 @@ private:
}; };
class PIThread;
typedef void (*ThreadFunc)(void * );
// FIXME: remove that!
class StdFunctionThreadFuncAdapter { class StdFunctionThreadFuncAdapter {
public: public:
static void threadFuncStdFunctionAdapter(void* it); static void threadFuncStdFunctionAdapter(void* it);

View File

@@ -133,8 +133,11 @@ public:
bool direction; bool direction;
}; };
#ifdef PIP_CXX11_SUPPORT
typedef std::function<void(KeyEvent, void *)> KBFunc;
#else
typedef void (*KBFunc)(KeyEvent, void * ); typedef void (*KBFunc)(KeyEvent, void * );
#endif
//! Constructs keyboard listener with external function "slot" and custom data "data" //! Constructs keyboard listener with external function "slot" and custom data "data"
explicit PIKbdListener(KBFunc slot = 0, void * data = 0, bool startNow = true); explicit PIKbdListener(KBFunc slot = 0, void * data = 0, bool startNow = true);
@@ -151,6 +154,11 @@ public:
//! Set external function to "slot" //! Set external function to "slot"
void setSlot(KBFunc slot) {ret_func = slot;} void setSlot(KBFunc slot) {ret_func = slot;}
#ifdef PIP_CXX11_SUPPORT
//! Set external function to "slot"
void setSlot(std::function<void(KeyEvent)> slot) {ret_func = [slot](KeyEvent e, void *){slot(e);};}
#endif
//! Returns if exit key if awaiting //! Returns if exit key if awaiting
bool exitCaptured() const {return exit_enabled;} bool exitCaptured() const {return exit_enabled;}

View File

@@ -172,6 +172,21 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay)
} }
#ifdef PIP_CXX11_SUPPORT
PIThread::PIThread(std::function<void ()> 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() { PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this); PIINTROSPECTION_THREAD_NEW(this);
tid_ = -1; tid_ = -1;
@@ -533,3 +548,11 @@ void PIThread::__thread_func_once__() {
_endThread(); _endThread();
} }
PIThread * PIThread::runOnce(std::function<void ()> func) {
PIThread * t = new PIThread();
t->setSlot(func);
t->startOnce();
return t;
}

View File

@@ -57,7 +57,11 @@ public:
static __PIThreadCollection_Initializer__ __PIThreadCollection_initializer__; static __PIThreadCollection_Initializer__ __PIThreadCollection_initializer__;
#ifdef PIP_CXX11_SUPPORT
typedef std::function<void(void *)> ThreadFunc;
#else
typedef void (*ThreadFunc)(void * ); typedef void (*ThreadFunc)(void * );
#endif
class PIP_EXPORT PIThread: public PIObject class PIP_EXPORT PIThread: public PIObject
{ {
@@ -68,6 +72,11 @@ public:
//! Contructs thread with custom data "data", external function "func" and main loop delay "loop_delay". //! 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); 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<void()> func, bool startNow = false, int loop_delay = -1);
#endif
//! Contructs thread with main loop delay "loop_delay". //! Contructs thread with main loop delay "loop_delay".
PIThread(bool startNow = false, int loop_delay = -1); PIThread(bool startNow = false, int loop_delay = -1);
@@ -86,8 +95,15 @@ public:
EVENT_HANDLER1(bool, start, int, timer_delay); EVENT_HANDLER1(bool, start, int, timer_delay);
bool start(ThreadFunc func) {return start(func, -1);} bool start(ThreadFunc func) {return start(func, -1);}
bool start(ThreadFunc func, int timer_delay) {ret_func = func; return start(timer_delay);} bool start(ThreadFunc func, int timer_delay) {ret_func = func; return start(timer_delay);}
#ifdef PIP_CXX11_SUPPORT
bool start(std::function<void()> func) {return start(func, -1);}
bool start(std::function<void()> func, int timer_delay) {ret_func = [func](void*){func();}; return start(timer_delay);}
#endif
EVENT_HANDLER0(bool, startOnce); EVENT_HANDLER0(bool, startOnce);
EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();} EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();}
//#ifdef PIP_CXX11_SUPPORT
// EVENT_HANDLER1(bool, startOnce, std::function<void()>, func) {ret_func = [func](void*){func();}; return startOnce();}
//#endif
EVENT_HANDLER0(void, stop) {stop(false);} EVENT_HANDLER0(void, stop) {stop(false);}
EVENT_HANDLER1(void, stop, bool, wait); EVENT_HANDLER1(void, stop, bool, wait);
EVENT_HANDLER0(void, terminate); EVENT_HANDLER0(void, terminate);
@@ -98,6 +114,11 @@ public:
//! \brief Set external function that will be executed after every \a run() //! \brief Set external function that will be executed after every \a run()
void setSlot(ThreadFunc func) {ret_func = func;} 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<void()> func) {ret_func = [func](void*){func();};}
#endif
//! \brief Set priority of thread //! \brief Set priority of thread
void setPriority(PIThread::Priority prior); void setPriority(PIThread::Priority prior);
@@ -134,6 +155,10 @@ public:
EVENT(started) EVENT(started)
EVENT(stopped) EVENT(stopped)
#ifdef PIP_CXX11_SUPPORT
static PIThread * runOnce(std::function<void()> func);
#endif
//! \handlers //! \handlers
//! \{ //! \{