How to handle same socket in different threads?

爱⌒轻易说出口 提交于 2020-01-25 12:59:05

问题


I am trying to handle socket in different threads creating runtime failure. See following code.

void MySocket::Lock()
{
    m_LockCount++;

    if( m_LockCount )
    {
        CSocket::Create( 8080 );
    }
}

void MySocket::Unlock()
{
    m_LockCount--;

    if( !m_LockCount )
    {
        CSocket::Close();
    }
}

I am calling Lock() from one thread and Unlock() from other. When it executes CSocket::Close() it gives an exception.

I googled for this bug and got some reasons. This happens because; a CSocket object should be used only in the context of a single thread because the SOCKET handle encapsulated by a CAsyncSocket object is stored in a per-thread handle map. They are also suggesting a solution by sharing SOCKET handles between threads (http://support.microsoft.com/kb/175668). But this is not possible in my case since I am excepting some notification callback which will not work with above solution. Can anybody suggest a mechanism to share CSocket among threads without effecting notification callbacks?


回答1:


If, as you say, "a CSocket object should be used only in the context of a single thread," then there is no "mechanism to share CSocket among threads".

In other words, one of the threads needs to own the CSocket, and the others can't mess with it.

In such cases, the solution is to use an inter-thread messaging system. That way, one of the other threads can send a message to the owner saying, "Hey, buddy, close your socket!"

The details of how you would do that messaging depend entirely on the context of your program.




回答2:


You coould just use the socket directly and stop using the, obviously, flawed MFC implementation ...




回答3:


I would advise you to use some higher-level (and less buggy) socket API like Boost.Asio. Note that it does not make sockets thread-safe anyway (see there). You have to use some lock/unlock facility.

I am not sure i understand your question about sharing sockets among threads without using notification callbacks. Between threads T1 and T2, supposing T1 manages a socket, there are only two ways for T2 to become aware of a socket event. Either some notification launched by T1 or a question asked by T2 to T1, either on a regular basis or in a blocking call.



来源:https://stackoverflow.com/questions/1523916/how-to-handle-same-socket-in-different-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!