Qt moveToThread()vs调用新线程,当我们使用每个 [英] Qt moveToThread() vs calling new thread when do we use each

查看:1318
本文介绍了Qt moveToThread()vs调用新线程,当我们使用每个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们什么时候在线程应用程序中使用这个函数调用。
给出了在同一类中定义的两个函数fun1()和fun2(),它们处理数据到缓冲区中的读/写(队列操作)。实现多线程这些。我们必须在一个单独的线程中运行这两个函数。现在让我们说第一个函数read在它的线程的开始被调用。


是更好地使用moveTothread b $ b函数写在第一个
函数线程的开头



在新的
线程类中定义第二个函数,并在
调用该线程第一个线程的开始。


使用 moveToThread 我们可以更改对象的线程关联。 OP询问是如何在不同的线程中运行同一个类的两个函数。



让类 A 和两个函数 f1 code> f2

  class A 
{
public:
void f1();
void f2(int i);
void run(); //显示我们如何在不同的线程中触发f1和f2
}

Qt 已经提供了一个用于在不同线程中运行函数的类,它被称为 QtConcurrentRun


QtConcurrent :: run()函数在一个单独的线程中运行一个函数。
函数的返回值通过 QFuture
API提供。


被触发的函数可以是外部函数或成员函数。所以在我们的例子中,如果我们想从对象本身开始在不同的线程中启动 f1 f2 run()

  
// QFuture< void>因为f1是void
QFuture< void> future1 = QtConcurrent :: run(this,& A :: f1);
int k = 5; //使用参数并发运行
QFuture< void> future2 = QtConcurrent :: run(this,& A :: f2,k);类似地,你可以同时执行任何类的任何公共函数,例如)


$ b >

  QImage image = ...; 
QFuture< void> future = QtConcurrent :: run(image,& QImage :: invertPixels,QImage :: InvertRgba);

A a;
QFuture< void> future1 = QtConcurrent :: run(A,& A :: f1);

注意两个调用之间的区别:



< blockquote>

QtConcurrent :: run()也接受指向成员函数的指针。
第一个参数必须是const引用或指向类的
实例的指针。当
调用const成员函数时,通过const引用传递是有用的;传递指针对
调用修改实例的非const成员函数非常有用。


并发执行的功能已完成,您应该使用 QFutureWatcher


When do we use each of this function calls in a threaded application. given two functions fun1() and fun2() defined in the same class dealing with read/write of data into buffers(queue operation). to achieve multi-threading to these. we would have to run the two functions in a separate thread. now lets say the first function read is called at the start of its thread.

is it better to use moveTothread ( second thread)for function write at the start of the first functions thread

Or

define the second function in a new thread class and call that thread at the start of the first thread.

解决方案

Using moveToThread we can change the thread affinity of an object. What the OP asks is how we can run two functions of the same class in different threads.

Let class A and two functions f1 and f2

class A
{
public:
    void f1();
    void f2(int i); 
    void run(); // shows how we can trigger f1 and f2 in different threads
}

Qt already provided a class for running functions in different threads and it is called QtConcurrentRun

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

The function that is triggered can be either an external function or a member function. So in our case if we wanted from the object itself to start f1 and f2 in different threads we could do the following in run()

void run()
{
   // QFuture<void> because f1 is void 
   QFuture<void> future1 = QtConcurrent::run(this, &A::f1);
   int k = 5; // Concurrent run with arguments
   QFuture<void> future2 = QtConcurrent::run(this, &A::f2, k);
} 

similarly you could execute any public function of any class concurrently, eg

QImage image = ...;
QFuture<void> future = QtConcurrent::run(image, &QImage::invertPixels, QImage::InvertRgba);

A a;
QFuture<void> future1 = QtConcurrent::run(A, &A::f1);

Notice the difference between the two calls:

QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.

In order to check when a concurrently executed function has finished you should use a QFutureWatcher.

这篇关于Qt moveToThread()vs调用新线程,当我们使用每个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆