I have an application that needs to download a variable list of files (changes based on user, what's changed, etc). The list can be very short or very long (1000's of files). I start X worker threads with a list for each thread to download. If I run 1 thread, it all works fine. If I run >1 thread, it "may" crash in __acrt_lock (I don't call that, however). The problem is on the winsock connect call. If I comment out that call, it works (obviously does not download the file, but it doesn't crash).
THIS USED TO WORK UNDER THE OLD v110 COMPILER CHAIN. I have upgraded to the v141 chain, and now the problem is occurring. I am using the multi-threaded libraries, of course.
I have "shortcut" the code with premature return statements in various places, and have determined that the single line calling the winsock function "connect" is the problem. There are no global variables used (only private local storage to the thread).
bool Socket::connect(const char * adrs, int port) {
lastErrCode = 0;
myIP = adrs;
myPort = port;
if (inet_addr(adrs) == INADDR_NONE) {
getHostByName(adrs, myIP);
}
else {
myIP = adrs;
}
if ((me = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
lastErrCode = WSAGetLastError();
return (false);
}
SOCKADDR_IN sock;
sock.sin_family = PF_INET;
sock.sin_port = htons(port);
sock.sin_addr.s_addr = inet_addr(myIP.str());
return(false); //:DEBUG:
if (::connect(me, (SOCKADDR*)&sock, sizeof(SOCKADDR)) == SOCKET_ERROR) {
lastErrCode = WSAGetLastError();
closesocket(me);
me = INVALID_SOCKET;
return (false);
}
return (true);
}