将参数传递给 QThreadPool::globalInstance()->start() 的问题
problem with passing argument to QThreadPool::globalInstance()->start()
所以我在 AntiFlood 中有这个成员函数 class:
void AntiFlood::unBan()
{
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
}
我想将它传递给:
threadpool.globalInstance()->开始(解除禁令);
不起作用 - 结果错误:没有匹配函数调用 'QThreadPool::start()' threadpool.globalInstance()->start(unBan);
^;
但另一方面,如果我使用 lambda:
auto lam = [this, room, nick](){
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
};
threadpool.globalInstance()->start(lam);
它工作正常。
我如何将 void AntiFlood::unBan( ) 传递给 threadpool.globalInstance()->start(),这需要 std::function functionToRun?
您看到的基本问题是 AntiFlood::unBan
是(或至少 'appears to be')一个 non-static 成员函数。在这种情况下,必须针对 AntiFlood
类型的有效对象调用它。因为 QThreadPool::start
有签名...
void QThreadPool::start(std::function<void ()> functionToRun, int priority = 0)
你需要给它传递一个 'self contained' std::function<void()>
这正是你所做的...
auto lam = [this, room, nick]()
{
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
};
threadpool.globalInstance()->start(lam);
通过在 lambda 中捕获 this
。
简而言之,我会说您目前做事的方式是 correct/accepted 方式。
所以我在 AntiFlood 中有这个成员函数 class:
void AntiFlood::unBan()
{
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
}
我想将它传递给: threadpool.globalInstance()->开始(解除禁令);
不起作用 - 结果错误:没有匹配函数调用 'QThreadPool::start()' threadpool.globalInstance()->start(unBan); ^; 但另一方面,如果我使用 lambda:
auto lam = [this, room, nick](){ QThread::msleep(5000); std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n"; sendIT(lineToPost); }; threadpool.globalInstance()->start(lam);
它工作正常。
我如何将 void AntiFlood::unBan( ) 传递给 threadpool.globalInstance()->start(),这需要 std::function
您看到的基本问题是 AntiFlood::unBan
是(或至少 'appears to be')一个 non-static 成员函数。在这种情况下,必须针对 AntiFlood
类型的有效对象调用它。因为 QThreadPool::start
有签名...
void QThreadPool::start(std::function<void ()> functionToRun, int priority = 0)
你需要给它传递一个 'self contained' std::function<void()>
这正是你所做的...
auto lam = [this, room, nick]()
{
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
};
threadpool.globalInstance()->start(lam);
通过在 lambda 中捕获 this
。
简而言之,我会说您目前做事的方式是 correct/accepted 方式。