Fix segfault from missing mutex lock

master
Substitute 1 year ago
parent acfdcf887a
commit b73591bacf
  1. BIN
      Project1
  2. 9
      modernize/sockets.cpp
  3. 2
      project/server.h

Binary file not shown.

@ -7,11 +7,15 @@
constexpr auto max_buffer = 1024;
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
{
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
{
auto& my_queue = message_queue[socket];
@ -102,6 +106,7 @@ auto net::epilogue() noexcept -> void { WSACleanup(); }
#else
auto net::closesocket(const SOCKET socket) noexcept -> unsigned
{
std::unique_lock<std::recursive_mutex> lock(mutex());
message_queue.erase(socket);
return close(socket);
}

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

Loading…
Cancel
Save