我有我自己的DLL被注入到另一个进程中。从另一个进程中,DLL通过boost::message_queue将IPC消息发送到我的应用程序。我使用的std :: stringstream的构造消息,如下:std :: stringstream错误?
class Client
{
...
private:
template <class T> void AddMessageParameter(const T &m)
{
_message << m << "|";
}
void SendMessage()
{
if (_mq && _message.str().length() < 1024) {
// Do not send the same message again.
if (_mq_last_sent_message != _message.str()) {
_mq_last_sent_message = _message.str();
try {
unsigned int tries = 0;
// Try send the message five times before giving up.
do {
if (_mq->try_send(_mq_last_sent_message.c_str(), _mq_last_sent_message.length(), 0))
tries = 5;
else
::Sleep(128);
++tries;
} while (tries < 5);
} catch (...) {
// TODO: Add log4cxx logging here for errors...
}
}
}
// Clear the message for a new one.
_message.seekp(0);
_message.clear();
_message.str(std::string());
}
private:
std::stringstream _message;
std::string _mq_last_sent_message;
boost::shared_ptr<boost::interprocess::message_queue> _mq;
};
在DLL中,该函数的一个发送以下消息不断:
AddMessageParameter("CLIENT__TABLE__PLAYER_BANKROLL");
AddMessageParameter(window_handle);
AddMessageParameter(seat);
AddMessageParameter(s);
SendMessage();
现在,这会产生这样的消息CLIENT_TABLE_PLAYER_BANKROLL|00211606|6|€1.28|
。问题是,在每几千条消息中,第一个参数不会在那里添加,并且消息变得像00211606|6|€1.28|
这是为什么?这是std :: stringstream中的一些错误,或者我可能做错了什么?
在此先感谢您的帮助。
编辑:
问题解决了。这是一个线程安全问题。一个简单的互斥体解决了这个问题。
+1对于旁注 – onof 2010-07-27 11:40:58
另一个为旁注+1。 – 2010-07-27 12:35:20