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

@@ -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() {
PIINTROSPECTION_THREAD_NEW(this);
tid_ = -1;
@@ -533,3 +548,11 @@ void PIThread::__thread_func_once__() {
_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__;
#ifdef PIP_CXX11_SUPPORT
typedef std::function<void(void *)> 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<void()> 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<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_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_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<void()> 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<void()> func);
#endif
//! \handlers
//! \{