承插式跨螺纹锁定

问题描述

| 在我的异步套接字的ReceiveCallBack中将Lock()那里的套接字好吗?我问是因为有可能另一个线程同时在套接字上发送数据。
private void ReceiveCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;

lock(client)
{
    int bytesRead = client.EndReceive(ar);
    // do some work

    // Kick off socket to receive async again.
    client.BeginReceive(state.buffer,StateObject.BufferSize,new AsyncCallback(ReceiveCallback),state);
}
}    

// This is commonly called by another thread
public void SendMessage(string cmdName,Object data)
{
    lock (client)
    {
         client.Send(arrayofdata,arraylength,0);
    }
}
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)