我在"pthread vs std :: thread"和"QThread vs pthread"上看到了不同的主题,但在"std :: thread vs QThread"中没有.
我必须编写一个软件来驱动3D打印机并需要使用线程.将有一个线程将不断检查安全性,另一个用于执行打印过程,一些用于分别驱动每个硬件组件(运动,喷射......)等等...该程序是用Windows开发的C++ 11/QT.
首先我想使用QThread,但在我看来,QThread不允许你像std :: thread那样做很多东西,例如,在阅读Anthony Williams的"C++ Concurrency in Action"时,我看到它是可能的要求std :: thread通过执行类似于std::thread t1(&Class::function, this, ...);QThread似乎无法执行的操作从另一个线程执行函数.
我想拥有的机制是一种说法,如果我想在当前线程或另一个线程中执行一个函数.
您会选择哪一个以及为什么?
我必须从 QGraphicsScene 中的模型渲染行列表(> 400000)。由于这个操作很长,我尝试在另一个线程中进行渲染操作。
注意:我的所有类都是模板,因为我渲染不同类型的模型,每种类型具有不同的视图转换
首先,我从主线程创建 QGraphicsView :
template<>
GCode3DView<Core::PrintingModel>::GCode3DView(int width, int height, QWidget* parent)
: QGraphicsView(parent),
m_scene(new GCode3DScene<Core::PrintingModel>(this)),
m_result(),
m_handler(),
m_yRatio(2.0/3)
{
int w = width;
int h = height;
setScene(m_scene);
QTransform rotation;
rotation.rotate(30, Qt::XAxis);
setTransform(rotation);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setTransformationAnchor(QGraphicsView::NoAnchor);
setCacheMode(QGraphicsView::CacheNone);
setInteractive(false);
m_scene->setSceneSize(w,h);
m_scene->setSceneRect(*sceneRect*);
}
Run Code Online (Sandbox Code Playgroud)
当模型(行列表)准备好时,我从主线程调用此函数:
template<class T>
void GCode3DView<T>::setSceneModel(const T *model)
{
m_result = QPixmap();
m_scene->setModel(model); //Creates QGraphicsItems if not enough, disables the one that will be unused (see the function below if needed)
QtConcurrent::run(this,&GCode3DView<T>::saveImage, model);
}
Run Code Online (Sandbox Code Playgroud)
在调用并发运行之前,我相信场景的每个项目都属于主线程。然后从并发线程调用 saveImage,执行以下操作: …