QtCurrent与成员函数和线程池兼容
QtConcurrent with member function and thread pool
我可以使用QtConcurrent::run在单独的线程中运行成员函数。我使用以下代码:
QFuture<MyObject> future = QtConcurrent::run(this, &MyClass::doSomething, param1, param2);
现在我想使用一个自定义的QThreadPool,而不是全局的,因为这个任务的线程数量应该是可配置的。根据文档,我尝试了以下代码,但不幸的是,它找不到合适的重载。
QThreadPool pool;
QFuture<MyObject> future = QtConcurrent::run(&pool, this, &MyClass::doSomething, param1, param2);
是否支持此功能?
AFAIK、QThreadPool
和QtConcurrent
不能混合在一起
如果要使用QThreadPool
,则需要定义QRunnable
的实例,该实例可以在其run()
函数中调用函数MyClass::doSomething
。
/* code not tested */
class RunnableHelper: public QObject, public QRunnable
{
Q_OBJECT
/* param1, param2 storage */
public:
explicit RunnableHelper(param1, param2);
void run()
{
MyClass::doSomething(param1, param2);
}
};
RunnableHelper* rh= new RunnableHelper(param1, param2);
QThreadPool::globalInstance()->start(rh);
聚会迟到了一点;还
这是可能的。我所做的是
QThreadPool *pool;
pool = new QThreadPool() ;
.
.
QFuture<MyObject> future = QtConcurrent::run(pool, ...);
相关文章:
- 静态 constexpr 类成员变量对多线程读取是否安全?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 如何将类的成员函数传递给线程
- 成员变量在多线程 C++ 时自行更改
- 线程消息传递或更好:在"大师班"中访问其他班级的成员
- 非静态成员失败的线程调用函数
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 锁定来自其他线程的类成员
- C++多线程程序:变量定义为类成员的隔离错误
- 当我在C++中调用 struce 的只读静态成员时,线程是否安全
- 线程不是 std c++ 的成员
- 多个线程可以读取同一个类成员变量吗?
- std::使用类成员函数创建线程 - 最佳实践
- std::mutex作为一个成员变量对多个线程来说是安全的吗
- 线程中的成员函数指针
- 如何使用C 类的线程成员变量
- 线程成员函数和mex文件因std::bad_alloc而崩溃
- C++多线程成员变量
- 线程c++成员函数模板可变模板
- c++中的线程成员函数