Qt从另一个成员函数并发运行成员函数(Qt Concurrent run member function from another member function)

编程入门 行业动态 更新时间:2024-10-27 12:30:47
Qt从另一个成员函数并发运行成员函数(Qt Concurrent run member function from another member function)

我想在一个单独的线程中启动一个成员函数,从另一个成员调用它。 也许下面的代码更清晰。

有一个按钮可以在一个线程中启动计数器,它可以工作:

void MainWindow::on_pushButton_CountNoArgs_clicked() { myCounter *counter = new myCounter; QFuture<void> future = QtConcurrent::run(counter, &myCounter::countUpToThousand); }

MyCounter类成员函数:

void myCounter::countUpToHundred() { for(int i = 0; i<=100; i++) { qDebug() << "up to 100: " << i; } } void myCounter::countUpToThousand() { for(int i = 0; i<=1000; i++) { qDebug() << "up to 1000: " << i; if (i == 500) { //here I want to launch myCounter::countUpToHundred() in another thread } } }

提前致谢。

I would like to launch a member function in a separate thread calling it from another member. Maybe the code below is clearer.

There is a button which launches the counter in a thread and it works:

void MainWindow::on_pushButton_CountNoArgs_clicked() { myCounter *counter = new myCounter; QFuture<void> future = QtConcurrent::run(counter, &myCounter::countUpToThousand); }

MyCounter class member functions:

void myCounter::countUpToHundred() { for(int i = 0; i<=100; i++) { qDebug() << "up to 100: " << i; } } void myCounter::countUpToThousand() { for(int i = 0; i<=1000; i++) { qDebug() << "up to 1000: " << i; if (i == 500) { //here I want to launch myCounter::countUpToHundred() in another thread } } }

Thanks in advance.

最满意答案

假设你想要并行运行2个计数器,你有3个线程:

线程1:UI-Thread(或主线程)

这里运行on_pushButton_CountNoArgs_clicked() 。 你不应该在这个功能上做艰苦的工作,因为如果你想要达到每秒60帧,那么你所有的工作只需要16毫秒。 要启动一个新线程来运行countUpToThousand()是个好主意。

线程2:后台线程(由QtConcurrent启动,运行countUpToThousand)

这与线程1并行运行,并且您正在使用myCounter的相同实例(即内存中的相同位置),因此请注意您读取和写入的成员变量。

线程3:后台线程(由QtConcurrent启动,运行countUpToHundred)

开始使用(正如汉克指出)

void myCounter::countUpToThousand() { for(int i = 0; i<=1000; i++) { qDebug() << "up to 1000: " << i; if (i == 500) { QtConcurrent::run(this, &myCounter::countUpToHundred); } } }

这将与线程1和线程2并行运行。

现在你可能得到疯狂的输出结果,如988\n99\n当一个计数器是999而另一个是88时,因为线程2和线程3将同时打印到控制台而不关心其他线程是在做。

另请注意,在线程2和线程3完成之前,您不能删除counter ,因为您仍然会尝试访问内存,您的应用程序可能会崩溃。

Assuming you want to run the 2 counters parallel, you have 3 threads:

Thread 1: UI-Thread (or main thread)

Here runs on_pushButton_CountNoArgs_clicked(). You should not do hard work in this function because if you want to achive 60 frames per second, you only have 16 ms for all the work. To starting a new thread to run countUpToThousand() is a good idea.

Thread 2: background thread (started by QtConcurrent, running countUpToThousand)

This runs in parallel to Thread 1, and you are working with the same instance of myCounter (i.e. the same place in memory) so be careful which member variables you read and write.

Thread 3: background thread (started by QtConcurrent, running countUpToHundred)

Start using (as hank pointed out)

void myCounter::countUpToThousand() { for(int i = 0; i<=1000; i++) { qDebug() << "up to 1000: " << i; if (i == 500) { QtConcurrent::run(this, &myCounter::countUpToHundred); } } }

This will run in parallel to Thread 1 and Thread 2.

Now you might get crazy output results like 988\n99\n when one counter is at 999 and the other is at 88 because Thread 2 and Thread 3 will be printing to console at the same time and don't care about what the other thread is doing.

Also note that you must not delete counter before Thread 2 and Thread 3 are done because of you do, the'll still try to access the memory and your application will probably crash.

更多推荐

本文发布于:2023-07-19 02:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   成员   Qt   Concurrent   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!