我在生產者執行緒中遍歷檔案,把檔案塞入佇列,消費者從佇列取檔案路徑進行復制、或剪切行為
用了 QSemaphore 來限制佇列長度,達到阻塞的目的,起了8個消費者執行緒
這段代碼復制是沒問題,剪切(rename)就會崩潰,為什么?rename 是異步的嗎?
報錯的故障模塊是
故障模塊名稱: msvcrt.dll
static QSemaphore freeSpace(8);
static QSemaphore usedSpace(0);
static QQueue<MoveInfo *> task_q;
// 消費者代碼
void Consumer::run()
{
MoveInfo *info;
while(true)
{
usedSpace.acquire();
info = task_q.dequeue();
freeSpace.release();
if(info->src.isEmpty())
{
info->deleteLater();
break;
}
if(!QFileInfo(info->dst).exists())
{
if(info->remove)
{
bool b = QFile::rename(info->src, info->dst);
b = QFile::rename(info->src.replace(".sa", ".sc"), info->dst.replace(".sa", ".sc"));
} else {
QFile::copy(info->src, info->dst);
QFile::copy(info->src.replace(".sa", ".sc"), info->dst.replace(".sa", ".sc"));
}
}
info->deleteLater();
}
}
uj5u.com熱心網友回復:
好像問題就出在洗掉上uj5u.com熱心網友回復:
我感覺整段代碼都不是我喜歡的寫法。在你代碼基礎上是改不了了,要么我重新寫一份?
您好好說說您的需求?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/10801.html
標籤:Qt
