我有型STD的缓冲::双端队列。 有一个线程来写进去,另外一个从中读取数据,而最后一个来处理一些条件,这在缓存项转发。
我只是想从3个线程安全地访问该缓冲区。 是啊,我很初学者:-)
我创建了一个互斥我每次访问的缓冲时间,我包通过此访问
myMutex. lock() ;
// access here
myMutex. unlock() ;
另外,我用的std ::线程MyThread的(这一点,与FN)来创建线程。 我呼吁this_thread ::睡眠()经常以降低CPU负荷
我的问题是我Exeption说,中止()被调用! 当调试失败当我打电话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;
}
FN的工作线程的
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();
}
}
我知道这是一个很大的线程存在的,但它只是一个任务!