PIThreadPoolWorker ready to use

This commit is contained in:
2026-03-25 10:59:32 +03:00
parent 5868e0ec9d
commit a16f629dc5
4 changed files with 106 additions and 64 deletions

View File

@@ -1,30 +1,53 @@
#include "pip.h"
#include "pithreadpoolexecutor.h"
class A: public PIObject {
PIOBJECT(A)
public:
A(PIString n = {}) { setName(n); }
void foo() {
100_ms .sleep();
piCoutObj << "foo!";
100_ms .sleep();
}
};
int main(int argc, char * argv[]) {
PIVector<A *> objects;
objects << new A("1") << new A("2") << new A("3");
PITimer status_timer;
PIThreadPoolWorker pool(1);
int64_t id = -1;
status_timer.start(10_Hz, [&id, &pool] { piCout << "[timer] status" << (int)pool.taskStatus(id); });
200_ms .sleep();
id = pool.enqueueTask([](int64_t id) {
piCout << "[task ] start, id" << id;
300_ms .sleep();
piCout << "[task ] done";
});
piCout << "[main ]" << "enqueued, id" << id;
200_ms .sleep();
piCout << pool.removeTask(id);
piCout << pool.removeTask(id);
piCout << "[main ]" << "start";
PIThreadPoolWorker pool(2);
pool.start();
// int64_t id = -1;
// status_timer.start(10_Hz, [&id, &pool] { piCout << "[timer] status" << (int)pool.taskStatus(id); });
100_ms .sleep();
// pool.enqueueTask([](int64_t id) {
// piCout << "[task ] start, id" << id;
// // 500_ms .sleep();
// piCout << "[task ] done";
// });
pool.enqueueTask(objects[0], &A::foo);
pool.enqueueTask(objects[1], &A::foo);
pool.enqueueTask(objects[1], &A::foo);
pool.enqueueTask(objects[2], &A::foo);
10_ms .sleep();
delete objects[1];
objects.remove(1);
// piCout << "[main ]" << "enqueued, id" << id;
// 200_ms .sleep();
piCout << "[main ]" << "wait ...";
piCout << "[main ]" << "wait done";
1000_ms .sleep();
pool.stopAndWait();
status_timer.stopAndWait();
piDeleteAll(objects);
return 0;
}