从3个线程访问std :: deque

时间:2014-03-02 01:14:04

标签: c++ multithreading mutex

我有一个std :: deque类型的缓冲区。 有一个写入它的线程,另一个用于读取它的线程,最后一个用于处理缓冲区中哪个项目转发的条件。

我只想安全地从3个线程访问此缓冲区。是的,我是非常初学者:-)

我创建了一个互斥锁,每次访问缓冲区时都会通过

包装此访问
myMutex. lock() ;
// access here
myMutex. unlock() ;

另外,我使用std :: thread myThread(this,& fn)来创建线程。我经常调用this_thread :: sleep()来减少cpu负载

我的问题是我得到了Exeption说abort()已被调用!当我调用myThread.join()时调试失败 什么是错误!!

编辑:已添加代码 这是我的主要发送功能

void UDPStreamSender::SendStream(const char* sendMsg, size_t size)
{

    cout << "---- Send Stream starts... ----" << endl;

    char* longMsg = new char[size];
    memcpy(longMsg, sendMsg, size);

    std::thread segThread(&UDPStreamSender::DoSegmentation, this, longMsg, size);
    _isRunning = true;
    std::thread sendThrad(&UDPStreamSender::SendBuffer, this);
    std::thread ackThrad(&UDPStreamSender::AckRecive, this);
    std::thread timeOutThread(&UDPStreamSender::ManageTimeout, this);


    sendThrad.join();
    ackThrad.join();
    timeOutThread.join();

    cout << "---- Send Stream done! ----" << endl;
}

关于工作线程

void UDPStreamSender::DoSegmentation(const char* longMsg, unsigned int size)
{
    Segment* cSeg = new Segment();
    cSeg->seqNum = lastSeqNum;

    msgLength = size;
    msgSegLen = segLength - SEQ_NUM_LEN;

    segmentsNumber = (unsigned int)ceil((float)msgLength / (msgSegLen));

    for (size_t i = 0; i < segmentsNumber; i++)
    {

        cSeg->seqNum++;
        lastSeqNum = cSeg->seqNum;

        cSeg->data = new char[msgSegLen];

        int sendMsgSegLen = msgSegLen;
        if (i == segmentsNumber - 1)
            sendMsgSegLen = msgLength - i*msgSegLen;

        memcpy(cSeg->data, longMsg + i*msgSegLen, sendMsgSegLen);

        // Add to send buffer
        while (sendBuffer->isFull())
        {
            this_thread::sleep_for(std::chrono::milliseconds(50));
        }
        cSeg->isSent = false;

        bufLock.lock();
        std::unique_lock<std::mutex> lock(bufLock);
        sendBuffer->Add(cSeg);
        bufLock.unlock();
    }
    cv.notify_all();
}

void UDPStreamSender::SendBuffer()
{
    bufLock.lock();
    bool hasElms = sendBuffer->hasElems();
    bufLock.unlock();

    while (_isRunning || hasElms)
    {
        bufLock.lock();
        size_t firstUnsent = sendBuffer->firstUnsent();
        size_t buffCount = sendBuffer->count();
        bufLock.unlock();

        if (firstUnsent == buffCount)
        {
            this_thread::sleep_for(std::chrono::milliseconds(50));
            continue;
        }
        for (size_t i = firstUnsent; i < buffCount; i++)
        {
            bufLock.lock();
            Segment sSeg = sendBuffer->at(i);
            int st = SendSegment(&sSeg);
            if (st >= segLength)
            {
                sSeg.isSent = true;
                DWORD j = GetTickCount();
                sSeg.timeOutTick = j + timeOutTicks;
                sendBuffer->Replace(i, &sSeg);
                sendBuffer->sendSeg();
                cout << "SEG sent: SeqNum=" << sSeg.seqNum << endl;
            }
            bufLock.unlock();
        }
        bufLock.lock();
        hasElms = sendBuffer->hasElems();
        bufLock.unlock();
    }
}

void UDPStreamSender::AckRecive()
{
    char* ackMessage;
    while (!_allRecived)
    {
        ackMessage = ackReciver->Recive();

        string ackMsg(ackMessage);
        if (ackMsg.substr(0, 3).compare("ACK") != 0)
            continue;

        unsigned short ackSeqNum = 0;
        memcpy(&ackSeqNum, ackMessage + 3, 2);

        cout << "ACK recieved: seqNum=" << ackSeqNum << endl;

        bufLock.lock();
        sendBuffer->Ack(ackSeqNum);
        _allRecived = !sendBuffer->hasElems();
        bufLock.unlock();
    }
}

void UDPStreamSender::ManageTimeout()
{
    bufLock.lock();
    bool hasElms = sendBuffer->hasElems();
    bufLock.unlock();

    while (hasElms)
    {
        bufLock.lock();

        DWORD segTick = sendBuffer->first().timeOutTick;
        DWORD cTick = GetTickCount();
        if (sendBuffer->hasElems() && cTick > segTick)
        { // timeout, resend all buffer
            sendBuffer->resendAll();
            cout << "Timeout: seqNum=" << sendBuffer->first().seqNum << endl;
        }
        bufLock.unlock();
        this_thread::sleep_for(std::chrono::milliseconds(50));

        bufLock.lock();
        hasElms = sendBuffer->hasElems();
        bufLock.unlock();
    }
}

我知道那里有很多线索,但它只是一个任务!

1 个答案:

答案 0 :(得分:2)

我试过了! 错误来自我锁定()和解锁()互斥锁的方式。

这是bufLock.lock(),使用std::unique_lock<std::mutex> mtx_lock(bufLock);锁定deque缓冲区。

还需要两个variable_condition来处理isEmpty和isFull以停止转发,添加到缓冲区。

最终代码,如果有人发现它有用。

注意:此代码占用大量内存并消耗CPU,请勿在没有修订的情况下使用它。欢迎帮助我:)

void UDPStreamSender::SendStream(const char* sendMsg, size_t size)
{
    InitializeNewSendSession();
    cout << "---- Send Stream starts... ----" << endl;

    char* longMsg = new char[size];
    memcpy(longMsg, sendMsg, size);

    std::thread segThread(&UDPStreamSender::DoSegmentation, this, longMsg, size);
    _isRunning = true;
    std::thread sendThrad(&UDPStreamSender::SendBuffer, this);
    std::thread ackThrad(&UDPStreamSender::AckRecive, this);
    std::thread timeOutThread(&UDPStreamSender::ManageTimeout, this);

    segThread.join();
    sendThrad.join();
    _isRunning = false;
    ackThrad.join();
    timeOutThread.join();

    cout << "---- Send Stream done! ----" << endl;
}

void UDPStreamSender::DoSegmentation(const char* longMsg, unsigned int size)
{
    Segment* cSeg = new Segment();
    cSeg->seqNum = lastSeqNum;

    msgLength = size;
    msgSegLen = segLength - SEQ_NUM_LEN;

    segmentsNumber = (unsigned int)ceil((float)msgLength / (msgSegLen));

    for (size_t i = 0; i < segmentsNumber; i++)
    {

        cSeg->seqNum++;
        lastSeqNum = cSeg->seqNum;

        cSeg->data = new char[msgSegLen];

        int sendMsgSegLen = msgSegLen;
        if (i == segmentsNumber - 1)
            sendMsgSegLen = msgLength - i*msgSegLen;

        memcpy(cSeg->data, longMsg + i*msgSegLen, sendMsgSegLen);

        // Add to send buffer
        std::unique_lock<std::mutex> mtx_lock(bufLock);
        while (sendBuffer->isFull())
        {
            stopifFull.wait(mtx_lock);
        }
        cSeg->isSent = false;

        sendBuffer->Add(cSeg);
        mtx_lock.unlock();
        stopIfEmpty.notify_all();
    }

}

int UDPStreamSender::SendSegment(const Segment* seg)
{
    char* sMsg = new char[segLength];
    sMsg[0] = NULL;

    memcpy(sMsg, (char*)&seg->seqNum, SEQ_NUM_LEN);
    memcpy(sMsg + SEQ_NUM_LEN, seg->data, msgSegLen);

    int st = streamSender->Send(sMsg, segLength);

    delete sMsg;
    return st;
}

void UDPStreamSender::SendBuffer()
{

    bufLock.lock();
    bool hasElms = sendBuffer->hasElems();
    bufLock.unlock();

    while (_isRunning || hasElms)
    {
        std::unique_lock<std::mutex> mtx_lock(bufLock);
        size_t firstUnsent = sendBuffer->firstUnsent();
        size_t buffCount = sendBuffer->count();

        while (!sendBuffer->hasElems())
        {
            stopIfEmpty.wait_for(mtx_lock, std::chrono::milliseconds(100));
            if (_allRecived)
                return;
        }

        for (size_t i = firstUnsent; i < buffCount; i++)
        {

            Segment sSeg = sendBuffer->at(i);
            int st = SendSegment(&sSeg);
            if (st >= segLength)
            {
                sSeg.isSent = true;
                DWORD j = GetTickCount();
                sSeg.timeOutTick = j + timeOutTicks;
                sendBuffer->Replace(i, &sSeg);
                sendBuffer->sendSeg();
                cout << "SEG sent: SeqNum=" << sSeg.seqNum << endl;
            }

        }

        hasElms = sendBuffer->hasElems();

        mtx_lock.unlock();
    }
}

void UDPStreamSender::AckRecive()
{
    char* ackMessage;
    while (!_allRecived)
    {

        ackMessage = ackReciver->Recive();

        string ackMsg(ackMessage);
        if (ackMsg.substr(0, 3).compare("ACK") != 0)
            continue;

        unsigned short ackSeqNum = 0;
        memcpy(&ackSeqNum, ackMessage + 3, 2);

        cout << "ACK recieved: seqNum=" << ackSeqNum << endl;

        std::unique_lock<mutex> mtx_lock(bufLock);
        sendBuffer->Ack(ackSeqNum);
        _allRecived = !sendBuffer->hasElems() || !_isRunning;
        mtx_lock.unlock();
        stopifFull.notify_one();

    }
}

void UDPStreamSender::ManageTimeout()
{
    bufLock.lock();
    bool hasElms = sendBuffer->hasElems();
    bufLock.unlock();

    while (!_allRecived)
    {

        std::unique_lock<mutex> mtx_lock(bufLock);
        while (!sendBuffer->hasElems())
        {
            stopIfEmpty.wait_for(mtx_lock, std::chrono::milliseconds(100));
            if (_allRecived)
                return;
        }

        DWORD segTick = sendBuffer->first().timeOutTick;
        DWORD cTick = GetTickCount();
        if (sendBuffer->hasElems() && cTick > segTick)
        { // timeout, resend all buffer
            sendBuffer->resendAll();
            cout << "Timeout: seqNum=" << sendBuffer->first().seqNum << endl;
        }

        hasElms = sendBuffer->hasElems();

        mtx_lock.unlock();
    }
}