Dlaczego ta referencja "nie działa" ?

0

Chodzi o to,że mam pewną strukturę i ją przesyłam do 2 klas i do wątku. W wątku chce zmienić wartość w strukturze dlatego przesyłam ją jako referencje. W tych dwóch klasach jest także referencja na tą strukturę . Jednak gdy zmienie wartość w wątku to w klasach te wartości są nie zmienione . Dlaczego ? Może pokaże to w kodzie .

main

int main()
{
	try
	{
		Server ServerInfo{};
		Database DatabaseInfo{};
		boost::asio::io_service io_service;

		boost::property_tree::ptree pt;
		boost::property_tree::ini_parser::read_ini("config.ini", pt);

		DatabaseInfo.host = pt.get<std::string>("DATABASE.HOST");
		DatabaseInfo.username = pt.get<std::string>("DATABASE.USER");
		DatabaseInfo.password = pt.get<std::string>("DATABASE.PASS");
		DatabaseInfo.schema = pt.get<std::string>("DATABASE.SCHEMA");

		ServerInfo.ip = pt.get<std::string>("LOGIN_SERVER.IP");
		ServerInfo.port = pt.get<int>("LOGIN_SERVER.PORT");
		ServerInfo.server_name = pt.get<std::string>("GAME_SERVER.NAME");

		std::shared_ptr<TDatabase> Database_ptr = std::make_shared<TDatabase>(DatabaseInfo);

		Database_ptr->Connect();

		TServer Server(io_service, Database_ptr, ServerInfo);

		std::shared_ptr<TCommands> commands = std::make_shared<TCommands>();
		std::shared_ptr<boost::thread> thread = std::make_shared<boost::thread>(boost::bind(&TCommands::ParseCommands,commands,ServerInfo));

		io_service.run();
	}

	catch (std::exception &e)
	{
		std::cerr << e.what() << std::endl;
	}

	std::cin.get();
	return 0;
}

TServer.cpp

 TServer::TServer(boost::asio::io_service &io_service, std::shared_ptr<TDatabase> database_ptr, Server &ServerInfo) :
Database_ptr(database_ptr), ServerInfo(ServerInfo), acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(ServerInfo.ip), ServerInfo.port))
{
	Accept_Connection();
}

TServer.h

class TServer
{
private:
	boost::asio::ip::tcp::acceptor acceptor;
	std::shared_ptr<TDatabase> Database_ptr;
	std::shared_ptr<TSession> Connection;
	Server &ServerInfo;
public:
	TServer(boost::asio::io_service &io_service, std::shared_ptr<TDatabase> database_ptr, Server &ServerInfo);

	void Accept_Connection();
	void Handle_Connection(std::shared_ptr<TSession> connection, const boost::system::error_code &error);
};

TSession.h

class TSession : public std::enable_shared_from_this<TSession>
{
private:
	std::shared_ptr<TSocket> sock_ptr;
	std::shared_ptr<TDatabase> Database_ptr;

	boost::asio::streambuf buf;
	std::vector<std::string> aPacket;

	Account account;
	Server &ServerInfo;
public:
	TSession(boost::asio::io_service &io_service, std::shared_ptr<TDatabase> database_ptr, Server &ServerInfo);

	std::shared_ptr<boost::asio::ip::tcp::socket> Socket();
	void Start();
	void ReadHandler(const boost::system::error_code &error, std::size_t bytes_transferred);
	void ParsePacket(const std::string packet);
	void Authentication();
};

TSession.cpp


TSession::TSession(boost::asio::io_service &io_service, std::shared_ptr<TDatabase> database_ptr, Server &ServerInfo) :
sock_ptr(std::make_shared<TSocket>(io_service)), ServerInfo(ServerInfo), Database_ptr(database_ptr)
{

}

TCommands.h

void TCommands::ParseCommands(Server &ServerInfo)
{
	std::string command = " ";
	std::vector<std::string> sTemp;
	while (getline(std::cin,command)) 
	{
		boost::algorithm::split(sTemp, command, std::bind2nd(std::equal_to<char>(), (' ')));
		std::size_t sTempsize = sTemp.size();
		if (sTempsize == 3) 
		{
			if (sTemp[0] == "set" && sTemp[1] == "maintenace" && (sTemp[2] == "on" || sTemp[2] == "off")) 
			{
				if ((sTemp[2] == "on" && ServerInfo.is_maintenace) || (sTemp[2] == "off" && !ServerInfo.is_maintenace))
				{
					std::cout << "It's already setted in on/off " << std::endl;
				}

				else
				{
					if (sTemp[2] == "on")
						ServerInfo.is_maintenace = true;
					else
						ServerInfo.is_maintenace = false;		
				}
			}
		}

		else 
		{
			std::cout << "Wrong command" << std::endl;
		}
	}
}
0

A ty chociaż uruchamiasz ten wątek? Bo mnie się wydaje, że musisz zrobić: thread->join(); po utworzeniu wątku.

0

A nie widzisz ? Mi wszystko z wątkiem działa normalnie.

0

Wątek się tworzy normalnie . Jest on na heapie bo ma działać w tle.

0

boost::ref
Btw, nie widzę nigdzie żebyś lockował dostęp do tej referencji, więc po użyciu boost::ref będziesz miał wyścig (aktualnie nie masz wyścigu bo bind domyślnie używa kopiowania). Zatem musisz wprowadzić jakąś sekcję krytyczną że to w ogóle mogło działać.

0

Dzięki . Po dodaniu boost::ref działa.Jakoś spróbuje to zmodyfikować.

1 użytkowników online, w tym zalogowanych: 0, gości: 1