Fix segfault from missing mutex lock

This commit is contained in:
Substitute 2021-02-15 15:06:48 -05:00
parent acfdcf887a
commit b73591bacf
3 changed files with 8 additions and 3 deletions

BIN
Project1

Binary file not shown.

View File

@ -7,11 +7,15 @@
constexpr auto max_buffer = 1024; constexpr auto max_buffer = 1024;
decltype(net::message_queue) net::message_queue{}; decltype(net::message_queue) net::message_queue{};
auto inline mutex() -> std::recursive_mutex&
{
static std::recursive_mutex mutex{}; //allows for multiple thread access to our recv.
return mutex;
}
auto net::recv(const SOCKET socket) noexcept -> std::string auto net::recv(const SOCKET socket) noexcept -> std::string
{ {
static std::recursive_mutex mutex{}; //allows for multiple thread access to our recv. std::unique_lock<std::recursive_mutex> lock(mutex());
std::unique_lock<std::recursive_mutex> lock(mutex);
try try
{ {
auto& my_queue = message_queue[socket]; auto& my_queue = message_queue[socket];
@ -102,6 +106,7 @@ auto net::epilogue() noexcept -> void { WSACleanup(); }
#else #else
auto net::closesocket(const SOCKET socket) noexcept -> unsigned auto net::closesocket(const SOCKET socket) noexcept -> unsigned
{ {
std::unique_lock<std::recursive_mutex> lock(mutex());
message_queue.erase(socket); message_queue.erase(socket);
return close(socket); return close(socket);
} }

View File

@ -12,7 +12,7 @@ class server : networked
{ {
auto client = networked(accept(socket, nullptr, nullptr)); auto client = networked(accept(socket, nullptr, nullptr));
client_connected.execute(client); client_connected.execute(client);
#ifdef THREADED #ifndef THREADED
std::thread(&server::accept_client_message, this, client).detach(); std::thread(&server::accept_client_message, this, client).detach();
#else #else
std::thread(&server::accept_client_message, this, client).join(); std::thread(&server::accept_client_message, this, client).join();