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

标签 multithreading qt c++11

我想渲染一个三角形,其坐标由单独的线程不断更新(使用 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

如何在此类中创建一个连续运行 updateCooperatives() 的新线程,同时通过 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/

相关文章:

c++ - 如何向 QFileSystemModel 添加自定义角色

python - Qt - 使用 lambda 将槽与参数连接

c++ - 将 move 语义与 std::pair 或 std::tuple 一起使用

c++ - 在 C++/Qt 中是否有将字符串转换为结构中的字段的方法?

java - 在java中非同步读取整数线程安全吗?

java - 创建一个以有限速率发出项目的 Flowable,以避免需要缓冲事件

java - 在多个线程中更新 AtomicInteger 变量时未获得预期结果

c++ - 如何通过 createWithSpriteFrameName 使用动态 Sprite 名称?

c++ - 使用 boost::pool_allocator 时不调用 move 构造函数

python - 队列和多处理