git-svn-id: svn://db.shs.com.ru/pip@888 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
44
main.cpp
44
main.cpp
@@ -345,30 +345,40 @@ class B: public PIObject {
|
|||||||
PIOBJECT(B)
|
PIOBJECT(B)
|
||||||
public:
|
public:
|
||||||
EVENT_HANDLER1(void, h1, MyType, v) {piCout << "handler h1" << v;}
|
EVENT_HANDLER1(void, h1, MyType, v) {piCout << "handler h1" << v;}
|
||||||
|
EVENT_HANDLER(void, th) {piCout << "handler th";}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
PIThread thread([](){piCout << "thread";}, true, 100);
|
|
||||||
piMSleep(2000);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
PITimer timer([](){piCout << "timer";});
|
|
||||||
//t.setSlot(test);
|
|
||||||
timer.addDelimiter(5, [](void * d){piCout << "delim 5";});
|
|
||||||
timer.addDelimiter(2, [](){piCout << "delim 2";});
|
|
||||||
timer.start(100);
|
|
||||||
piMSleep(2000);
|
|
||||||
|
|
||||||
A a;
|
|
||||||
B b;
|
B b;
|
||||||
CONNECTU_QUEUED(&a, e1, &b, h1, &b);
|
PIThread::runOnce([](){piCout << "thread 1"; piMSleep(1000);}, "first");
|
||||||
a.e1(PIVector<int>() << 1 << 2);
|
PIThread::runOnce([](){piCout << "thread 2"; piMSleep(500);}, "second");
|
||||||
b.callQueuedEvents();
|
PIThread::runOnce([](){piCout << "thread 3"; piMSleep(0);}, "third");
|
||||||
|
PIThread::runOnce(&b, "th", "first th");
|
||||||
|
PIThread::runOnce(&b, "th", "second th");
|
||||||
|
PIThread::runOnce(&b, "th", "third th");
|
||||||
|
piSleep(2);
|
||||||
return 0;
|
return 0;
|
||||||
|
//
|
||||||
|
//PITimer timer([](){piCout << "timer";});
|
||||||
|
//t.setSlot(test);
|
||||||
|
//timer.addDelimiter(5, [](void * d){piCout << "delim 5";});
|
||||||
|
//timer.addDelimiter(2, [](){piCout << "delim 2";});
|
||||||
|
//timer.start(100);
|
||||||
|
//piMSleep(2000);
|
||||||
|
|
||||||
|
//A a;
|
||||||
|
//B b;
|
||||||
|
//int iii = 10;
|
||||||
|
//CONNECTL(&a, e1, [&](MyType v){piCout << "lambda" << v << iii; ++iii;});
|
||||||
|
//std::function<void(MyType)> f = [&](MyType v){piCout << "lambda" << v << iii; ++iii;};
|
||||||
|
//piCout << typeid([&](MyType v){piCout << "lambda" << v << iii; ++iii;}).name();
|
||||||
|
//piCout << typeid(f).name();
|
||||||
|
//a.e1(PIVector<int>() << 1 << 2);
|
||||||
|
//piCout << iii;
|
||||||
|
//
|
||||||
|
//return 0;
|
||||||
|
|
||||||
/*for (int i = 0; i < 0x2; ++i) {
|
/*for (int i = 0; i < 0x2; ++i) {
|
||||||
PIByteArray ba, ba2;
|
PIByteArray ba, ba2;
|
||||||
|
|||||||
@@ -120,6 +120,22 @@ PIVector<PIThread * > __PIThreadCollection::threads() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void __PIThreadCollection::startedAuto(PIThread * t) {
|
||||||
|
PIMutexLocker _ml(auto_mutex);
|
||||||
|
auto_threads_ << t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void __PIThreadCollection::stoppedAuto() {
|
||||||
|
PIThread * t = emitter()->cast<PIThread>();
|
||||||
|
if (!t) return;
|
||||||
|
auto_mutex.lock();
|
||||||
|
auto_threads_.removeAll(t);
|
||||||
|
auto_mutex.unlock();
|
||||||
|
delete t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int __PIThreadCollection_Initializer__::count_(0);
|
int __PIThreadCollection_Initializer__::count_(0);
|
||||||
@@ -549,10 +565,26 @@ void PIThread::__thread_func_once__() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIThread * PIThread::runOnce(std::function<void ()> func) {
|
void PIThread::runOnce(PIObject * object, const char * handler, const PIString & name) {
|
||||||
PIThread * t = new PIThread();
|
PIThread * t = new PIThread();
|
||||||
t->setSlot(func);
|
t->setName(name);
|
||||||
|
if (!PIObject::piConnectU(t, PIStringAscii("started"), object, object, PIStringAscii(handler), "PIThread::runOnce")) {
|
||||||
|
delete t;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
__PIThreadCollection::instance()->startedAuto(t);
|
||||||
|
CONNECTU(t, stopped, __PIThreadCollection::instance(), stoppedAuto);
|
||||||
t->startOnce();
|
t->startOnce();
|
||||||
return t;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PIThread::runOnce(std::function<void ()> func, const PIString & name) {
|
||||||
|
PIThread * t = new PIThread();
|
||||||
|
t->setName(name);
|
||||||
|
t->setSlot(func);
|
||||||
|
__PIThreadCollection::instance()->startedAuto(t);
|
||||||
|
CONNECTU(t, stopped, __PIThreadCollection::instance(), stoppedAuto);
|
||||||
|
t->startOnce();
|
||||||
|
//return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,8 @@
|
|||||||
class PIThread;
|
class PIThread;
|
||||||
class PIIntrospectionThreads;
|
class PIIntrospectionThreads;
|
||||||
|
|
||||||
class PIP_EXPORT __PIThreadCollection {
|
class PIP_EXPORT __PIThreadCollection: public PIObject {
|
||||||
|
PIOBJECT(__PIThreadCollection)
|
||||||
public:
|
public:
|
||||||
static __PIThreadCollection * instance();
|
static __PIThreadCollection * instance();
|
||||||
void registerThread(PIThread * t);
|
void registerThread(PIThread * t);
|
||||||
@@ -40,9 +41,11 @@ public:
|
|||||||
PIVector<PIThread * > threads() const;
|
PIVector<PIThread * > threads() const;
|
||||||
void lock() {mutex.lock();}
|
void lock() {mutex.lock();}
|
||||||
void unlock() {mutex.unlock();}
|
void unlock() {mutex.unlock();}
|
||||||
|
void startedAuto(PIThread * t);
|
||||||
|
EVENT_HANDLER(void, stoppedAuto);
|
||||||
private:
|
private:
|
||||||
PIVector<PIThread * > threads_;
|
PIVector<PIThread * > threads_, auto_threads_;
|
||||||
mutable PIMutex mutex;
|
mutable PIMutex mutex, auto_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -155,8 +158,15 @@ public:
|
|||||||
EVENT(started)
|
EVENT(started)
|
||||||
EVENT(stopped)
|
EVENT(stopped)
|
||||||
|
|
||||||
|
//! \brief Start event handler with name \"handler\" of object \"object\"
|
||||||
|
//! in separate thread with name \"name\"
|
||||||
|
//! and automatically delete it on function finish
|
||||||
|
static void runOnce(PIObject * object, const char * handler, const PIString & name = PIString());
|
||||||
|
|
||||||
#ifdef PIP_CXX11_SUPPORT
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
static PIThread * runOnce(std::function<void()> func);
|
//! \brief Start function \"func\" in separate thread with name \"name\"
|
||||||
|
//! and automatically delete it on function finish
|
||||||
|
static void runOnce(std::function<void()> func, const PIString & name = PIString());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//! \handlers
|
//! \handlers
|
||||||
|
|||||||
Reference in New Issue
Block a user