下面的代码运行很好(即不丢弃消息)99.9的时间。但是,当数据报以微秒为单位在数据报之间以2-3微秒的速率进入时,我会遇到数据丢失。 boost notify_one()成员调用需要5到10微秒才能完成,因此在这些条件下它本身就是关键瓶颈。有关如何提高性能的任何建议?UDP - 在microbursts期间丢失数据
接收机/ “生产者” 代码螺纹:
if (bytes_recvd > 0) {
InQ.mut.lock();
string t;
t.append(data_, bytes_recvd);
InQ.msg_queue.push(t); // < 1 microsecs
InQ.mut.unlock();
InQ.cond.notify_one(); // 5 - 10 microsecs
}
消费者代码线程:
//snip......
std::string s;
while (1) {
InQ.mut.lock();
if (!InQ.msg_queue.empty()) {
s.clear();
s = InQ.msg_queue.front();
InQ.msg_queue.pop();
}
InQ.mut.unlock();
if (s.length()) {
processDatagram((char *)s.c_str(), s.length());
s.clear();
}
boost::mutex::scoped_lock lock(InQ.mut);
InQ.cond.wait(lock);
}
在linux中,你可以增加每个套接字的数据包的内核缓冲区,所以你不会丢失它们,我想你也可以在windows上做到这一点。 –
UDP作为标准不能保证传送。如果你想保证交付,使用TCP/IP。 – mjr
只是因为你使用UDP并不意味着你高兴地丢失了数据包。 –