I'm creating a small C++ server which is supposed to handle requests in an infinite loop like.
void Server::start_accept_loop(int server, SSL_CTX* ctx){
sockaddr_in incoming_addr;
socklen_t len = sizeof(incoming_addr);
int client_sock;
SSL* ssl;
while(Server::is_accepting){
client_sock = accept(server, (sockaddr*)&incoming_addr, len);
ssl = SSL_new(ctx);
SSL_set_fd(ssl, client_sock);
if (SSL_accept(ssl) == -1){
printf("Unable to accept ssl connection\n");
}else{
std::shared_ptr<Client> client (new Client(ssl));
std::thread th (background_client, client);
}
}
}
void Server::background_client(std::shared_ptr<Client> client_ptr){
std::shared_ptr<Client> client = client_ptr;
int request = 0;
request = client->get_request();
...
}
I have a few questions actually.
- When the created client shared_ptr goes out of scope after the thread creation does it decrement reference count and deallocate the object. It is a race condition with the new thread or is the reference count incremented at the
std::thread th (background_client, client);
line. Basically, I'm wondering if I need to wait some time before the while loop goes out of scope and create a new shared_ptr or if the reference count is incremented immediately? - My second question is about the SSL structure pointer. If the underlying client_sock and incoming_addr objects go out of scope without having been created dynamically on the heap, is it a problem for the SSL connection which probably relies on these or does the SSL pointer contain a conform dynamic copy of all this information?
- Is there anything else I should know?
Thank you.