From e6031c75976a8484574fd2127a017968a0bf1793 Mon Sep 17 00:00:00 2001 From: Mr_Goldberg Date: Fri, 17 Jan 2020 15:46:50 -0500 Subject: [PATCH] Fix possible thread issue. --- dll/steam_networking.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/dll/steam_networking.h b/dll/steam_networking.h index 601d2362..09809134 100644 --- a/dll/steam_networking.h +++ b/dll/steam_networking.h @@ -70,6 +70,8 @@ public ISteamNetworking std::recursive_mutex messages_mutex; std::vector messages; + + std::recursive_mutex connections_edit_mutex; std::vector connections; std::vector listen_sockets; @@ -79,11 +81,13 @@ public ISteamNetworking bool connection_exists(CSteamID id) { + std::lock_guard lock(connections_edit_mutex); return std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;}) != connections.end(); } struct Steam_Networking_Connection *get_or_create_connection(CSteamID id) { + std::lock_guard lock(connections_edit_mutex); auto conn = std::find_if(connections.begin(), connections.end(), [&id](struct Steam_Networking_Connection const& conn) { return conn.remote == id;}); if (connections.end() == conn) { @@ -98,13 +102,16 @@ struct Steam_Networking_Connection *get_or_create_connection(CSteamID id) void remove_connection(CSteamID id) { - auto conn = std::begin(connections); - while (conn != std::end(connections)) { - if (conn->remote == id) { + { + std::lock_guard lock(connections_edit_mutex); + auto conn = std::begin(connections); + while (conn != std::end(connections)) { + if (conn->remote == id) { - conn = connections.erase(conn); - } else { - ++conn; + conn = connections.erase(conn); + } else { + ++conn; + } } }