1.引入模块
QT += core gui concurrent
2.并发运行函数
将需要并行运行的部分封装成一个函数块
void analysis::cacheFile(QVector<mes*> glist_03,QVector<mes*>glist_04)
//是否有返回值,和是否传参看个人需要
{
for(int i = 0; i < glist_03.size(); i++){
qDebug()<<"glist_03"<< glist_03.at(i)->time;
}
for(int i = 0; i < glist_04.size(); i++){
qDebug()<<"glist_04"<< glist_04.at(i)->time;
}
}
3.启动并发模块
void analysis::test()
{
.
.
.
.
.
.
.
.
.
//有返回值
//QFuture<T> future = QtConcurrent::run(this, &analysis::cacheFile,glist8003,glist8004);
/*无返回值
*在需要的地方启动QtConcurrent::run()
* QtConcurrent::run(函数所在类对象名, &函数所在类名::函数名,参数1,参数2,...);
*/
QtConcurrent::run(this, &analysis::cacheFile,glist8003,glist8004);
}
4.结果
我做的是将缓存中数据通过QCustomPlot绘图,并在绘图的同时并不影响我将缓存中的数据循环写入txt文件中,二者并发运行。