Qt写的重命名程序,在使用OpenMP加速文件复制时出错
使用OpenMP的时候,循环次数不够。
QFileInfo info;
#pragma omp parallel for
for(int i=0;i<list.size();++i){
info=list.at(i);
QFile file(info.filePath());
if(info.fileName()=="."||info.fileName()==".."){
continue;
}
if(info.isDir()){ /*如果是文件夹则递归调用本函数*/
if(dstPath!=srcPath) dstDir.mkdir(info.fileName());
changeFileName(info.filePath(), dstPath + "/" + info.fileName(),selectWay);
continue;
}
QString fileName=info.baseName();
QString fileSuffix=info.completeSuffix();
if(selectBack==1){
fileName.append(addString);
}
else{
fileName.insert(0,addString);
}
fileName.append("."+fileSuffix);
if(dstPath!=srcPath) file.copy(dstPath+"/"+fileName);
else {
bool success=QFile::rename(dstPath+"/"+info.fileName(),dstPath+"/"+fileName);
if(!success){
qDebug()<<"replace fail";
done=0;
}
else{
qDebug()<<"success";
}
}
emit add();
}
本来是想看能加速多少,结果发现遇到文件夹递归调用时,有个文件夹用时0ms,就点进去看,发现并没有复制。重复试验了几次发现偶尔会发生。(之前维护了一个程序,把许多图片拼接起来,也是会少拼几个图片,当时不知道bug在哪,现在看来应该是openMP的问题)
完全不知道错误在哪
我的代码错误在哪呢?