Reputation: 377
I have a buffer of type std::deque. There is a thread to write into it, another one to read from it, and the last one to handle some conditions for which item in the buffer to forward.
I just want to access this buffer safely from the 3 threads. Yup, I'm very beginner :-)
I created a mutex and every time I access the buffer I wrap this access by
myMutex. lock() ;
// access here
myMutex. unlock() ;
Also, I used std::thread myThread(this, &fn) to create the threads. And I call this_thread::sleep() frequently to reduce cpu load
My problem is I got Exeption says abort() has been called! When I debug it fails when I call myThread.join() What is the fault!!
EDIT: code added this is my main sending function
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;
}
fn's on the worker threads
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();
}
}
I know that is a lot of threads there, but it's just an assignment !
Upvotes: 1
Views: 167
Reputation: 377
I have tried and tried! the error come form the way I lock() and unlock() the mutex.
That is insted bufLock.lock()
, use std::unique_lock<std::mutex> mtx_lock(bufLock);
to lock the deque buffer.
Also Two variable_condition is needed to handle the cases of isEmpty and isFull to stop forwarding, adding to the buffer.
The final code if anyone finds it useful.
NOTE: this code eats a lot memory and consume the CPU, don't use it without revision. And WELCOME to help me with it :)
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();
}
}
Upvotes: 2