gpt4 book ai didi

multithreading - 在构造函数中初始化的c++ 11线程以执行方法

转载 作者:行者123 更新时间:2023-12-03 13:00:13 35 4
gpt4 key购买 nike

我想渲染一个三角形,该三角形的坐标由一个单独的线程连续更新(使用m_offset)。该线程在整个运行时中并行运行。

我的应用程序使用GL小部件初始化了一个QT窗口。

#include "mainwindow.h"
#include <QApplication>
#include <iostream>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

GL小部件(glwidget.cpp)的定义如下:
#include "glwidget.h"

#include <iostream>
#include <thread>

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{

m_offset = 0.0

// HERE, I want to start a thread on updateCoordinates().
// Something like this: (this doesn't work)
//std::thread mythread (updateCoordinates);
// or this (getting segfault)
//std::thread mythread (&GLWidget::updateCoordinates, this);


}

void GLWidget::initializeGL()
{
glClearColor(0.2, 0.2, 0.2, 1);
}
void GLWidget::paintGL()
{


glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(-0.5+m_offset, -0.5+m_offset, 0);
glColor3f(0,1,0);
glVertex3f(0.5+m_offset, -0.5+m_offset, 0);
glColor3f(0,0,1);
glVertex3f(0.0+m_offset, 0.5+m_offset, 0);
glEnd();

}
void GLWidget::resizeGL(int w, int h)
{}


void GLWidget::updateCoordinates()
{

while(true)
{
m_offset += 0.0001;
}

}

这是对应的标题:
#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>

class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);

void initializeGL();
void paintGL();
void resizeGL(int w, int h);

void startSimulation();
void stopSimulation();
void updateCoordinates();

private:
double m_offset;

signals:

public slots:

};

#endif // GLWIDGET_H

我如何在该类中连续运行 updateCoordinates()的新线程中划出一个新线程,同时通过 paingGL()更新图形?

谢谢!

最佳答案

您可以使用QThread

虽然存在其他选项,例如QConcurrent,但有些选项为question its implementation

创建一个从QObject派生的对象,该对象将负责更新坐标。每次更新后,对象可以发出带有坐标的信号,然后在主线程上将其接收回来。

class Worker : public QObject 
{
Q_OBJECT

public:
Worker();
~Worker();

public slots:
void process();

signals:

typedef QVector<QPoint> PointList;

void NewCoordinates(PointList points);

private:
PointList m_pointList;

private:
};


Worker::Worker() {
// you could copy data from constructor arguments to internal variables here.
}

// --- DESTRUCTOR ---
Worker::~Worker() {
// free resources
}


// --- PROCESS ---
// Start processing data.
void Worker::process() {

// calculate new coordinates...

emit NewCoordinates(m_pointList);
}

从主线程设置并启动线程和工作程序实例。
QThread* thread = new QThread;
Worker* worker = new Worker();
worker->moveToThread(thread);

connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), worker, SLOT(process()));

// tidy up
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

// assuming we're in the MainWindow class, with a NewCoordinates slot function
// collect points - C++ 5 connect syntax
connect(worker, &Worker::NewCoordinates(Worker::PointList), this, &MainWindow::NewCoordinates(Worker::PointList);

// let's go
thread->start();

void MainWindow::NewCoordinates(Worker::PointList pointList)
{
// handle updated coordinates
}

要了解如何真正真正地使用QThread,有一篇很棒的文章 here

关于multithreading - 在构造函数中初始化的c++ 11线程以执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478753/

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