CSocket在多线程环境下使用Static库出错的解决办法
FIX: Unhandled Exception Using MFC Sockets in Visual C++ 6.0
The information in this article applies to:
The Microsoft Foundation Classes (MFC), when used with:
Microsoft Visual C++, 32-bit Enterprise Edition 6.0
Microsoft Visual C++, 32-bit Professional Edition 6.0
Microsoft Visual C++, 32-bit Learning Edition 6.0
Symptoms
When using MFC sockets in secondary threads in a statically linked MFC Visual C++ 6.0 application, an unhandled exception occurs.
Cause
The reason for the unhandled exception is that an object of type CMapPtrToPtr pointer, pointed to by m_pmapSocketHandle, is never created.
Resolution
The handle maps used by the sockets need to be created for each thread. The following code shows a function to do this:
void SocketThreadInit()
{
#ifndef
#define
#define
#endif
}
This function should be called once in each secondary thread before the first socket is created in the new thread.
在每个线程开始出调用SocketThreadInit函数,就没有问题了。