我有我自己的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