代码之家  ›  专栏  ›  技术社区  ›  AK_

.Net异步套接字多线程发送

  •  0
  • AK_  · 技术社区  · 15 年前

    我有一个插座,我用发送大缓冲区通过,如果我会这样做

    // may be accsed form a a lot of threads
    void Send(byte[] buffer)
    {
      m_socket.BeginSend(buffer ... , SendCallback);
    }
    
    void SendCallback()
    {
      m_socket.EndSend()
    
      // if not done sending- send the reset
      m_socket.BeginSend()
    }
    

    我的问题是:从多线程的角度来看,这是可行的,还是缓冲区交错?

    2 回复  |  直到 15 年前
        1
  •  0
  •   Henk Holterman    15 年前

    如果执行多个异步 在套接字上的操作,它们不会 他们开始了。

    但是,如果您是从多个线程发送数据块,那么您对“顺序”的定义是什么?

        2
  •  1
  •   Sam    15 年前

    看起来这是线程安全的。

    Simultaneously Executing Operations
    
    If your class supports multiple concurrent invocations, enable the developer to track each invocation separately by defining the MethodNameAsync overload that takes an object-valued state parameter, or task ID, called userSuppliedState. This parameter should always be the last parameter in the MethodNameAsync method's signature.
    

    http://msdn.microsoft.com/en-us/library/ms228974.aspx

    推荐文章