如何将我的函数放入线程QT并发

发布于 2025-02-06 09:53:29 字数 1006 浏览 3 评论 0原文

我具有这样的功能,

QList<MyObject*> list;

for (int i = 0; i < count; ++i)
{
    auto *object = new MyObject(this);
    ProcessFunc1(object);
    ProcessFunc2(object);
    
    ProcessFunc3(object); // a heavy function that I would like to parallelize

    list.push_back(object);
}

return list;

我需要正确地并行化此功能。最好的方法是什么?

我这样写了,但是我不确定是正确的:

QList<MyObject*> list;
QFutureSynchronizer<MyObject*> syncronizer;
for (int i = 0; i < count; ++i)
{
    auto future = QtConcurrent::run([this]() -> MyObject* {
        auto *object = new MyObject(this);
        ProcessFunc1(object);
        ProcessFunc2(object);
    
        ProcessFunc3(object); // a heavy function that I would like to parallelize
        return object;
});
    syncronizer.addFuture(future);
}

syncronizer.waitForFinished();

for (int i = 0; i < syncronizer.futures().count(); ++i)
    list.push_back(syncronizer.futures().at(i).result());

return list;

I have function like this

QList<MyObject*> list;

for (int i = 0; i < count; ++i)
{
    auto *object = new MyObject(this);
    ProcessFunc1(object);
    ProcessFunc2(object);
    
    ProcessFunc3(object); // a heavy function that I would like to parallelize

    list.push_back(object);
}

return list;

I need to correctly parallelize this function. What is the best way to do it?

I wrote it like this, but I'm not sure it's correct:

QList<MyObject*> list;
QFutureSynchronizer<MyObject*> syncronizer;
for (int i = 0; i < count; ++i)
{
    auto future = QtConcurrent::run([this]() -> MyObject* {
        auto *object = new MyObject(this);
        ProcessFunc1(object);
        ProcessFunc2(object);
    
        ProcessFunc3(object); // a heavy function that I would like to parallelize
        return object;
});
    syncronizer.addFuture(future);
}

syncronizer.waitForFinished();

for (int i = 0; i < syncronizer.futures().count(); ++i)
    list.push_back(syncronizer.futures().at(i).result());

return list;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2025-02-13 09:53:36
// create the objects in the main thread:
// creating objects in another thread can result in pain,
// see e.g. https://doc.qt.io/qt-6/qobject.html#thread-affinity
QList<MyObject *> list;
for (int i = 0; i < count; ++i)
    list.emplaceBack(this); // equivalent to list.append(new MyObject(this))
// "map" (== "apply") your function to all objects in parallel, wait for the result
QtConcurrent::blockingMap(list, [] {
    ProcessFunc1(object);
    ProcessFunc2(object);
    ProcessFunc3(object);
});
// here everything is done, since blockingMap waits for everything to finish
// create the objects in the main thread:
// creating objects in another thread can result in pain,
// see e.g. https://doc.qt.io/qt-6/qobject.html#thread-affinity
QList<MyObject *> list;
for (int i = 0; i < count; ++i)
    list.emplaceBack(this); // equivalent to list.append(new MyObject(this))
// "map" (== "apply") your function to all objects in parallel, wait for the result
QtConcurrent::blockingMap(list, [] {
    ProcessFunc1(object);
    ProcessFunc2(object);
    ProcessFunc3(object);
});
// here everything is done, since blockingMap waits for everything to finish
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文