Home > Mobile >  Qt multithreaded copying files QFile: : copy () a strange question
Qt multithreaded copying files QFile: : copy () a strange question

Time:10-15

I want to use the production consumption patterns, multithreaded copying files, using a static method QFile: : copy ()
Perform half would collapse, only copy the part of
If I were in QFile: : copy () the following add qDebug () & lt; <"Hello"; , that is, consumer, CPP line 24, will not collapse
 
QFile: : copy (SRC, DST);
QDebug () & lt; <"Hello";


What reason is this?

Producer. H

 

Struct MoveInfo {
Bool rm.
QString SRC.
QString DST.
};

Enum TAG {
COUNT_FILE=0,
HANDLE=1,
};

The static QSemaphore freeSpace (8);//control task queue length of buffer
The static QSemaphore usedSpace (0);
The static QQueue Task_q;


The class Producer: public QThread
{
Q_OBJECT

Public:
Producer (parent QObject *=nullptr);
~ Producer ();

Void the run ();
Void setParams (QString, QString, bool);

Signals:
Void mainMsgChanged (QString, QColor c=Qt: : black);
Void subMsgChanged (QString);
Void completed ();

Private:
QString root_path;
Int total=0;
Int count=0;
QString src_path;
QString dst_path;
Bool rm.
Void walkPath (QString, QString, TAG);
};



Producer. The CPP
 

Producer: : Producer (QObject * parent)
: QThread (parent)
{

}

Void Producer: : run ()
{
QDebug (). Noquote () & lt; <"The Source path:" & lt; Total=0;
count=0;
Emit mainMsgChanged (tr (" are statistical number... "));
WalkPath (src_path, dst_path, TAG: : COUNT_FILE);
QDebug (). Noquote () & lt; <"The Total number of files:" & lt; count=0;
Emit mainMsgChanged (tr (" being processed... "));
WalkPath (src_path, dst_path, TAG: : HANDLE).
Emit mainMsgChanged (tr (" Ready "));

If (total!=0)
Emit subMsgChanged (QString (" [1%] % % % 2/3 "). Arg (int (100 * ((float) count/total))). Arg (count). Arg (total));
The else
Emit subMsgChanged (QString (" [1%] % % % 2/3 "). Arg (100). Arg (count). Arg (total));

//no more documents, inform the consumer thread exit into the empty
MoveInfo info;
for (int i=0; i <8; I++)
{
FreeSpace. Acquire ();
Task_q. The enqueue (info);
UsedSpace. Release ();
}
}

Void Producer: : walkPath (QString SRC, QString DST, the TAG t)
{
The QDir src_dir (SRC);
The QDir dst_dir (DST);
QFileInfoList src_files=src_dir. EntryInfoList (QStringList {" *. XLS "}, QDir: : Files | QDir: : NoDotAndDotDot);
MoveInfo info;

If (t==TAG: : COUNT_FILE)
Emit subMsgChanged (QDir: : toNativeSeparators (SRC));//notification interface in the current directory

For (auto file: src_files)
{
If (t==TAG: : COUNT_FILE)//statistical file first total
{
Total++;
} else {
The info. The rm=rm;
Info. SRC=https://bbs.csdn.net/topics/file.filePath ();
Info. DST=dst_dir. FilePath (file. The fileName ());
FreeSpace. Acquire ();
Task_q. The enqueue (info);//the file information into queue
UsedSpace. Release ();
count++;
//notification interface update progress
Emit subMsgChanged (QString (" [1%] % % % 2/3 "). Arg (int (100 * ((float) count/total))). Arg (count). Arg (total));
}
}
QStringList src_dirs=src_dir. EntryList (QStringList {}, QDir: : Dirs | QDir: : NoDotAndDotDot);

//recursive processing all subdirectories
For (auto sd: src_dirs)
{
if(! Dst_dir. The exists (sd) & amp; & T==TAG: : HANDLE)
Dst_dir. Mkdir (sd);
WalkPath (src_dir filePath (sd), dst_dir. FilePath (sd), t);
}
}

Producer: : ~ Producer ()
{

}



Consumer. H consumers
 

The class Consumer: public QThread
{
Q_OBJECT
Public:
Consumer (parent QObject *=nullptr);
~ Consumer ();
Void the run ();
};


Consumer. CPP consumers
 

Consumer: : Consumer (QObject * parent)
: QThread (parent)
{

}

Void Consumer: : run ()
{
MoveInfo info;
QDebug () & lt;
While (true)
{
UsedSpace. Acquire ();
Info=task_q. Dequeue ();//get a file information
FreeSpace. Release ();

If (info. SRC. IsEmpty ())//if the file is called an empty, interruption, an end to
break;

if(! QFileInfo: : the exists (info. DST))
{
QFile: : copy (info. SRC, info. DST);
QDebug () & lt; }

If (info. Rm)
QFile: : remove (info. SRC);
}
}

Consumer: : ~ Consumer ()
{

}


 
Void MainWindow: : start ()
{
MProducer. SetParams (" D:/files ", "D:/target/", false);
MProducer - & gt; Start ().
for (int i=0; i <8; I++)
{
Consumer * c=new Consumer ();
The connect (c, & amp; Consumer: : finished, c, & amp; Consumer: : deleteLater);
C - & gt; Start ().
}
}
  •  Tags:  
  • Qt
  • Related