gpt4 book ai didi

c++ - 成员函数的 moveToThread

转载 作者:太空狗 更新时间:2023-10-29 23:03:09 25 4
gpt4 key购买 nike

是否可以将成员函数移动到新线程而不是整个类?

将其置于上下文中:

    class FooBar : public QObject {
Q_OBJECT
// some other function that deals with the UI changes
private:
get foo();
};

是否可以只将 foo() 移动到一个新线程?整个类有很多 UI 调用和其他可能不是线程安全的组件。我将非常感谢任何帮助,否则我将不得不重新组织类并将 foo() 移动到一个新对象。

最佳答案

您不能像这样将函数移动到线程。 moveToThread 更改 affinity该对象到该线程。这或多或少意味着该对象的信号将被发送到新线程的事件循环。

我认为您正在为此类任务寻找 QtConcurrent:

The QtConcurrent::run() function runs a function in a separate thread. The return value of the function is made available through the QFuture API.

 extern void aFunctionWithArguments(int arg1, double arg2, const QString &string);

int integer = ...;
double floatingPoint = ...;
QString string = ...;

QFuture<void> future = QtConcurrent::run(aFunctionWithArguments, integer, floatingPoint, string);

参见 Qt Concurrent FrameworkQFuture了解更多详情。

关于c++ - 成员函数的 moveToThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26655994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com