diff --git a/dlib/sockets/sockets_kernel_1.cpp b/dlib/sockets/sockets_kernel_1.cpp index 5bca6d073..73418ea8c 100644 --- a/dlib/sockets/sockets_kernel_1.cpp +++ b/dlib/sockets/sockets_kernel_1.cpp @@ -136,6 +136,8 @@ namespace dlib try { + // lock this mutex since gethostbyname isn't really thread safe + auto_mutex M(sockets_kernel_1_mutex::startup_lock); // if no hostname was given then return error if ( hostname.empty()) @@ -193,6 +195,8 @@ namespace dlib try { + // lock this mutex since gethostbyaddr isn't really thread safe + auto_mutex M(sockets_kernel_1_mutex::startup_lock); // if no ip was given then return error if (ip.empty()) diff --git a/dlib/sockets/sockets_kernel_2.cpp b/dlib/sockets/sockets_kernel_2.cpp index 4095c58b4..910fdbd2f 100644 --- a/dlib/sockets/sockets_kernel_2.cpp +++ b/dlib/sockets/sockets_kernel_2.cpp @@ -197,12 +197,10 @@ namespace dlib int n ) { - // ensure that WSAStartup has been called and WSACleanup will eventually - // be called when program ends - sockets_startup(); - try { + // lock this mutex since gethostbyname isn't really thread safe + auto_mutex M(sockets_kernel_2_mutex::startup_lock); // if no hostname was given then return error if ( hostname.empty()) @@ -254,12 +252,10 @@ namespace dlib std::string& hostname ) { - // ensure that WSAStartup has been called and WSACleanup will eventually - // be called when program ends - sockets_startup(); - try { + // lock this mutex since gethostbyaddr isn't really thread safe + auto_mutex M(sockets_kernel_2_mutex::startup_lock); // if no ip was given then return error if (ip.empty())