程序运行一段时间就出问题。目前已经知道是因为QMutex的问题,将这mutex.lock和mutex.unlock去掉就没问题(测试了5遍就是这个问题)。 先介绍下程序,程序基本过程如下:
1)socket线程从服务器获取
数据;
2)socket将数据发给data线程解析数据,分别是快递单号expressId,小车号carrierId;
3)data将快递单号发给http线程,http根据快递单号获取到格口boxid;
4)http将boxid发回给data,data将数据expressid,carrierId和boxid发给
数据库、plc和
界面。
因为expressId、carrierid和boxid不是同时获取,为了防止在获得boxid的同时,expressId和carrierid被socket下一波数据覆盖掉,导致数据不一致,我在访问expressId和carrierid之前先加了锁mutex.lock,在获取boxid并且将数据发给plc和界面及数据库之后解锁mutex.unlock。其中mutex是data的成员变量,加锁和解锁在两个不同的槽函数里。 
程序总共五个
线程,一个主线程
GUI,一个socket线程,一个data和数据库线程,一个http线程,一个plc线程。 
代码片段一、socket获取数据 void Socket::readServer()
{
    qDebug() << "-------------------------readServer--------------";
    startTime = QTime::currentTime();
    currentSocket = qobject_cast<QTcpSocket *>(sender());
    message = currentSocket->readAll();
    handleIndex = map.value(currentSocket);
    emit dataFromSocketSignal(message, handleIndex, startTime);  
 将数据发给data线程    qDebug() << message;
}
二、data获取并解析数据 void DataHandle::reciveDataFromSocket(
QString message, int handleIndex, QTime startTime)
{
    qDebug() << __FILE__ << __LINE__ << __FUNCTION__;
    mutex.lock(); 
 //这里加锁    carrierId = message.mid(carrierIdLength-3, 3);
    expressId = message.mid(carrierIdLength);
    this->startTime = startTime;
    this->handleIndex = handleIndex;
    emit getBoxidSignal(expressId);
 //将数据发给http}
三、http获取expressId并且得到boxid void Network::httpInRequest(QString expressId)
{ 
//接受数据并且向http请求    qDebug() << "----------------------------" << __LINE__ << __FUNCTION__ << "-----------------------";
    initInPortStr();
    index = inPortStr.indexOf("billCode");
    inPortStr.insert(index + 11, expressId);
    setTimeStamp();
    index = inPortStr.indexOf("requestTime");
    inPortStr.insert(index + 13, timeStamp);
    qDebug() << __LINE__ <<  qPrintable(inPortStr);
    url.setUrl(inPortStr);
    request.setUrl(url);
    reply = manager->get(request);
    connectSignalSlot();
}  
void Network::httpRead()
{ 
//读取http响应,即获得boxidqDebug() << "----------------" << __func__ << __LINE__ << "-----------------";
    responseStr = reply->readAll();
qDebug() << qUtf8Printable(responseStr);
    beginBracketIndex = responseStr.indexOf("[");
    endBracketIndex = responseStr.indexOf("]");
    if (endBracketIndex - beginBracketIndex == 1)
        boxId = "";
    else
        boxId = responseStr.mid(beginBracketIndex + 2, endBracketIndex - beginBracketIndex - 3);
qDebug() << "格口:" << qPrintable(boxId);
    emit boxIdSignal(boxId); 
//将数据boxid又发回给data}
 四、data从http获取返回的数据void DataHandle::getBoxId(QString boxId)
{
    ......
    emit plcDataSignal(carrierId.toShort(), boxId.toShort(), expressId, handleIndex, startTime);
  //发送carrierId、expressid给pLc线程    ....
    emit mainWindowDataSignal(carrierId, expressId, boxId, viewer); 
//将数据发给主线程界面    ....
    db.insert(sql); 
 //将数据插入数据库     mutex.unlock();
 //最后解锁,整个过程完成} 
问题描述:下面是打印出的数据 -------------------------readServer--------------  //socket读取数据"*#11173117237677906"
..\expressSortSystemZTRepair20190803\data.cpp 43 reciveDataFromSocket  //data接受数据---------------------------- 62 httpOutRequest -----------------------  //http接受data的数据并发送http请求73 
http://172.31.28.11:8090/branchweb/sortservice?data={"billCode":"73117237677906","goodsHeight":0,"goodsLength":0,"goodsVolume":0,"goodsWidth":0,"incomeClerk":"","lineType":"","nextSiteId":0,"pipeline":"59771-003","requestTime":1564798858000,"scaleSn":"","scanMan":"","sortMode":"sorting","trayCode":"050","turnNumber":10,"weight":0}&data_digest=&msg_type=WCS_SORTING_INFO&company_id=zto
---------------- httpRead 82 -----------------  //http读取响应的数据boxId{"message":"","result":{"billCode":"73117237677906","errorCode":"","isComp":false,"pipeline":"","scaleSn":"","sortPortCode":["255"],"sortSource":"0","trayCode":"050"},"status":true,"statusCode":"SUCCESS"}
格口: 255
getBoxId 54 ------------------getBoxId-------------------   //data从http获取数据并且发给PLC等"INSERT INTO express_outflow_zt(EXPRESSFLOW_ID, STATUS, EXPRESS_SN, BOX_NUM, DELIVERY_TYPE, CREATE_TIME, User_id)VALUES('73117237677906', 0, '73117237677906', '255', 0, '2019-08-03 10:20:58', 'tato')" "111" "255" "73117237677906" 2 "内轨1"
135 getBoxId
plcWrite 111 73117237677906 255 2  
//数据写入PLCfeedback urlStr: 
http://172.31.28.11:8090/branchweb/sortservice?data={"billCode":"73117237677906","pipeLine":"59771-003","sortPortCode":"255","sortSource":"0","sortTime":1564798858708,"turnNumber":1}&data_digest=&msg_type=WCS_SORTING_RESULT&company_id=zto
-------------------------readServer--------------   
//socket读取数据"*#11275164930244502"
..\expressSortSystemZTRepair20190803\data.cpp 43 reciveDataFromSocket  
//data接受数据---------------------------- 62 httpOutRequest -----------------------  
 //http接受data的数据并发送http请求73 
http://172.31.28.11:8090/branchweb/sortservice?data={"billCode":"75164930244502","goodsHeight":0,"goodsLength":0,"goodsVolume":0,"goodsWidth":0,"incomeClerk":"","lineType":"","nextSiteId":0,"pipeline":"59771-003","requestTime":1564798860000,"scaleSn":"","scanMan":"","sortMode":"sorting","trayCode":"050","turnNumber":10,"weight":0}&data_digest=&msg_type=WCS_SORTING_INFO&company_id=zto
---------------- httpRead 82 -----------------   
//http读取响应的数据boxId{"message":"","result":{"billCode":"75164930244502","errorCode":"","isComp":false,"pipeline":"","scaleSn":"","sortPortCode":["255"],"sortSource":"0","trayCode":"050"},"status":true,"statusCode":"SUCCESS"}
格口: 255
getBoxId 54 ------------------getBoxId-------------------  
//data从http获取数据并且发给PLC等plcWrite 112 75164930244502 255 2   
//数据写入PLC"INSERT INTO express_outflow_zt(EXPRESSFLOW_ID, STATUS, EXPRESS_SN, BOX_NUM, DELIVERY_TYPE, CREATE_TIME, User_id)VALUES('75164930244502', 0, '75164930244502', '255', 0, '2019-08-03 10:21:00', 'tato')" "112" "255" "75164930244502" 2 "内轨1"
feedback urlStr: 
http://172.31.28.11:8090/branchweb/sortservice?data={"billCode":"75164930244502","pipeLine":"59771-003","sortPortCode":"255","sortSource":"0","sortTime":1564798860708,"turnNumber":1}&data_digest=&msg_type=WCS_SORTING_RESULT&company_id=zto
135 getBoxId
-------------------------readServer--------------  下一波开始 不断重复,但是,运行运行着就下面这样了就只读socket服务器了,后面就没了  "*#11475162853265075"
-------------------------readServer--------------
"*#11475162868879886"
-------------------------readServer--------------
"*#11173117237677906*#11275164930244502"    //这里数据连了
-------------------------readServer--------------
"*#11373117924080879*#11473117232028807"
plcWrite 114 73116654255618 255 2
-------------------------readServer--------------
"*#11478106488260665"
-------------------------readServer--------------
"*#11475162774107456"
-------------------------readServer--------------
"*#11473116629520024"
-------------------------readServer--------------
"*#11473116654255618*#11475162853265075"
-------------------------readServer--------------
"*#11475162868879886"
-------------------------readServer--------------
"*#11173117237677906"
-------------------------readServer-------------- 
关闭的时候,data线程也退不了 
MainWindow::~MainWindow()
{
    qDebug() << __func__ << __LINE__; 
    delete ui;
    socket->deleteLater();
    network->deleteLater();
    data->deleteLater();
    plc->deleteLater(); 
    qDebug() << __func__ << __LINE__;
    socketThread->quit();
    socketThread->wait();
    qDebug() << __func__ << __LINE__;
    httpThread->quit();
    httpThread->wait();
    qDebug() << __func__ << __LINE__;
    dataThread->quit();  
    dataThread->wait();  
到这里就停了    qDebug() << __func__ << __LINE__;
//    plcThread->quit();
//    plcThread->wait(1000);
    if (plcConnectSuccess) {
        plcThread->quit();
        plcThread->wait();
    } else {
        exit(0);
    }
    qDebug() << __func__ << __LINE__;  
//  exit(0);
} 
线程同步不是很懂,用的也少,难道我用错了?