فهرست منبع

Protects against concurrent access to in game console texts.

Frank Zago 16 سال پیش
والد
کامیت
cbf2ed6b7a
2فایلهای تغییر یافته به همراه10 افزوده شده و 0 حذف شده
  1. 8 0
      client/GUIClasses.cpp
  2. 2 0
      client/GUIClasses.h

+ 8 - 0
client/GUIClasses.cpp

@@ -2967,6 +2967,8 @@ void CInGameConsole::show(SDL_Surface * to)
 
 
 	std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
 	std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
 
 
+	texts_mx.lock();
+
 	for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
 	for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
 	{
 	{
 		SDL_Color green = {0,0xff,0,0};
 		SDL_Color green = {0,0xff,0,0};
@@ -2986,16 +2988,22 @@ void CInGameConsole::show(SDL_Surface * to)
 	{
 	{
 		texts.erase(toDel[it]);
 		texts.erase(toDel[it]);
 	}
 	}
+
+	texts_mx.unlock();
 }
 }
 
 
 
 
 void CInGameConsole::print(const std::string &txt)
 void CInGameConsole::print(const std::string &txt)
 {
 {
+	texts_mx.lock();
+
 	texts.push_back(std::make_pair(txt, SDL_GetTicks()));
 	texts.push_back(std::make_pair(txt, SDL_GetTicks()));
 	if(texts.size() > maxDisplayedTexts)
 	if(texts.size() > maxDisplayedTexts)
 	{
 	{
 		texts.pop_front();
 		texts.pop_front();
 	}
 	}
+
+	texts_mx.unlock();
 }
 }
 
 
 void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
 void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)

+ 2 - 0
client/GUIClasses.h

@@ -7,6 +7,7 @@
 #include "FunctionList.h"
 #include "FunctionList.h"
 #include <set>
 #include <set>
 #include <list>
 #include <list>
+#include <boost/thread/shared_mutex.hpp>
 
 
 #ifdef max
 #ifdef max
 #undef max
 #undef max
@@ -532,6 +533,7 @@ class CInGameConsole : public IShowActivable, public KeyInterested
 {
 {
 private:
 private:
 	std::list< std::pair< std::string, int > > texts; //<text to show, time of add>
 	std::list< std::pair< std::string, int > > texts; //<text to show, time of add>
+	boost::mutex texts_mx;		// protects texts
 	std::vector< std::string > previouslyEntered; //previously entered texts, for up/down arrows to work
 	std::vector< std::string > previouslyEntered; //previously entered texts, for up/down arrows to work
 	int prevEntDisp; //displayed entry from previouslyEntered - if none it's -1
 	int prevEntDisp; //displayed entry from previouslyEntered - if none it's -1
 	int defaultTimeout; //timeout for new texts (in ms)
 	int defaultTimeout; //timeout for new texts (in ms)