-
Notifications
You must be signed in to change notification settings - Fork 2
Home
dora edited this page Apr 4, 2016
·
37 revisions
Welcome to the LThreadPool wiki! https://github.com/dora-BYR/ThreadPool/wiki #Usage ##Sample 1 // sample 2 auto task2 = LTask::create(= { LLOG("i'm a sub thread task 2"); int count = 0; do { LLOG("i'm task 2"); count = count + 1; } while (count < 8); }); task2->commit();
// sample 3
auto pool = LThreadPool::getInstance();
pool->pushTask([=]() {
LLOG("i'm a sub thread task 3");
int count = 0;
do {
LLOG("i'm task 3");
count = count + 1;
} while (count < 12);
});
##Sample 2 // sample 1 auto task1 = LTask::create(= { LLOG("i'm a sub thread task 1"); do { LLOG("i'm task 1"); } while (1); }); task1->commit(true); // 'true' - new thead created.
// sample 2
auto pool = LThreadPool::getInstance();
const char * name = "longhui";
int age = 25;
std:string desc("master of BUPT");
TaskDataParam * dataParam = new TaskDataParam();
dataParam->pushString("name", std::string(name));
dataParam->pushNumber("age", age);
dataParam->pushString("desc", std::string(desc));
pool->pushCommonTask([=](TaskDataParam * _dataParam) {
LLOG("i'm a sub thread task 2");
do {
LLOG("i'm task 2");
LLOG("name = %s", _dataParam->getStringValue("name").c_str());
LLOG("age = %ld", _dataParam->getNumberValue("age"));
LLOG("desc = %s", _dataParam->getStringValue("desc").c_str());
} while (1);
}, dataParam);
// sample 3
pool->pushTask([=]() {
LLOG("i'm a sub thread task 3");
do {
LLOG("i'm task 3");
} while (1);
});
pool->addThread(1);
#Tips:
- Get the thread-pool instance. there will be 1 default sub thread created.
- If you want more sub-thread to do your task, you can use 'pool->addThread(count)' or 'task->commit(true)' to create a new sub-thread.
- Every sub-thread will do scanning the task-queue all the time when it's idle.
single-thread&multi-task / multi-thread&multi-task supported.
See the test code for more information please.