改变锁的位置,解决多线程下载错误的问题
This commit is contained in:
parent
be0b63c209
commit
5ad594a028
@ -149,6 +149,7 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f
|
|||||||
vec[threadCount-1].second = fileSize; // 余数部分加入最后一个
|
vec[threadCount-1].second = fileSize; // 余数部分加入最后一个
|
||||||
|
|
||||||
qint64 bytesReceived = 0; // 下载接收的总字节数
|
qint64 bytesReceived = 0; // 下载接收的总字节数
|
||||||
|
QMutex lock;
|
||||||
// 任务队列
|
// 任务队列
|
||||||
auto mapCaller = [&, this](const QPair<qint64, qint64>& pair) -> qint64 {
|
auto mapCaller = [&, this](const QPair<qint64, qint64>& pair) -> qint64 {
|
||||||
QEventLoop event;
|
QEventLoop event;
|
||||||
@ -159,7 +160,6 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f
|
|||||||
request.setRawHeader("Range", QString("bytes=%1-%2").arg(pair.first).arg(pair.second).toLocal8Bit());
|
request.setRawHeader("Range", QString("bytes=%1-%2").arg(pair.first).arg(pair.second).toLocal8Bit());
|
||||||
QNetworkReply *reply = mgr.get(request);
|
QNetworkReply *reply = mgr.get(request);
|
||||||
qint64 writePos = pair.first;
|
qint64 writePos = pair.first;
|
||||||
QMutex lock;
|
|
||||||
qDebug() << "开始下载数据:" << QString(" %1~%2 -> writePos Start %3")
|
qDebug() << "开始下载数据:" << QString(" %1~%2 -> writePos Start %3")
|
||||||
.arg(pair.first).arg(pair.second).arg(writePos);
|
.arg(pair.first).arg(pair.second).arg(writePos);
|
||||||
connect(reply, &QNetworkReply::readyRead, [&lock, &writePos, &file, &bytesReceived, reply](){
|
connect(reply, &QNetworkReply::readyRead, [&lock, &writePos, &file, &bytesReceived, reply](){
|
||||||
@ -167,8 +167,8 @@ void Widget::multiDownload(const QString &url, qint64 fileSize, const QString &f
|
|||||||
{
|
{
|
||||||
QMutexLocker mLock(&lock);
|
QMutexLocker mLock(&lock);
|
||||||
// qDebug() << "调整文件的数据指针:" << writePos;
|
// qDebug() << "调整文件的数据指针:" << writePos;
|
||||||
// file.seek(writePos);
|
file.seek(writePos);
|
||||||
// file.write(data);
|
file.write(data);
|
||||||
bytesReceived += data.size();
|
bytesReceived += data.size();
|
||||||
qDebug() << QString("%1, %2, %3")
|
qDebug() << QString("%1, %2, %3")
|
||||||
.arg(writePos).arg(bytesReceived).arg(data.size());
|
.arg(writePos).arg(bytesReceived).arg(data.size());
|
||||||
|
Loading…
Reference in New Issue
Block a user