Eugene Eugene - 1 month ago 16
C++ Question

Boost thread breakpoint

I'm using visual studio 2015.

I have main loop, where I handle new connections to server:

void Server::acceptorLoop()
{
acceptor = new boost::asio::ip::tcp::acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 8001));//boost::asio::ip::address::from_string("xx.xx.xx.xx"), 80)

std::cout << "Waiting for clients..." << std::endl;

while (TRUE)
{
boost::asio::ip::tcp::socket* clientSock = new boost::asio::ip::tcp::socket (service);
acceptor->accept(*clientSock);
std::cout << "New client joined! " << std::endl;

Client* new_client = new Client; //create new client
new_client->clientSock = clientSock;
new_client->last_read_request = "";
new_client->activity = TRUE;
new_client->request_thread = new boost::thread(&Server::HandleRequest, this, new_client);//new thread with handling client requests

mtx.lock();
clients_list->push_back(new_client);
mtx.unlock();
std::cout << "Total clients:" << clients_list->size() << std::endl;
}
}


After new socket connected new HandleRequest thread starts. Variable iterator is for pinging connection with socket (it dropping to zero if any data from clients received).
My problem is:
Before I start program I can set breakpoint on iterator ad, but after it starts breakpoint dropped to if statement ("if (Client_to_handle->clientSock->available())"). Moreover if I make breakpoint for example on "if (Client_to_handle->clientSock->available())" and stop program there, then debugger will say that variable iterator is not defined. And program never hit "else if (iterator >= 10)" it just go through it to boost sleep func.
Why I can't define variable and my program refuses to see else if statement?

void Server::HandleRequest(Client * Client_to_handle)
{
int iterator = 0;
while (TRUE)
{
if (Client_to_handle->clientSock->available())
{
//do stuff and drop iterator to zero (connection active)
iterator = 0;
}
else if (iterator >= 10) //program refuses to get in here
{
if (iterator == 20)
{
Client_to_handle->clientSock->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
Client_to_handle->clientSock->close();
Client_to_handle->activity = FALSE;
}
boost::asio::write(*Client_to_handle->clientSock, boost::asio::buffer("1|"));
}
boost::this_thread::sleep(boost::posix_time::millisec(1000));
}
}

Answer

(I would like to comment, but do not have the rights yet!)

Are you sure that you are running a proper "debug" build where you still have all symbols left in place? It can happen, that you are having compiler optimizations enabled, which can lead to changing the order of statements and can completely eliminate variables.

I leave the link to the VC++ optimizations flags: https://msdn.microsoft.com/en-us/library/k1ack8f1.aspx?f=255&MSPPError=-2147217396

Check you compiler flags and see if you have any kind of /O in there.