title: How to share tokio::net::TcpStream act on it concurrently?
body: <p>I have a requirement to send and receive normal data on the same <code>TcpStream</code>, while sending heartbeat data at regular intervals. In the current implementation, I used <code>Arc&lt;Mutex&lt;TcpStream&gt;&gt;</code>, but it compiled with errors:</p>
  <pre><code>use anyhow::Result;
  use std::sync::{Arc, Mutex};
  use std::thread;
  use std::time::Duration;
  use tokio::io::{AsyncReadExt, AsyncWriteExt};
  use tokio::net::TcpStream;
  
  #[tokio::main]
  async fn main() -&gt; Result&lt;()&gt; {
      let stream = TcpStream::connect(&quot;127.0.0.1:8888&quot;).await.unwrap();
      let stream = Arc::new(Mutex::new(stream));
  
      let common_stream = stream.clone();
      let handler1 = tokio::spawn(async {
          loop {
              let mut stream = common_stream.lock().unwrap();
              let mut buf = [0u8; 10];
              stream.read_exact(&amp;mut buf).await.unwrap();
              buf.reverse();
              stream.write(&amp;buf).await.unwrap();
          }
      });
  
      let heartbeat_stream = stream.clone();
      let handler2 = tokio::spawn(async {
          loop {
              let mut stream = heartbeat_stream.lock().unwrap();
              stream.write_u8(1).await.unwrap();
  
              thread::sleep(Duration::from_millis(200));
          }
      });
  
      handler1.await?;
      handler2.await?;
  
      Ok(())
  }
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>error: future cannot be sent between threads safely
     --&gt; src\main.rs:14:20
      |
  14  |     let handler1 = tokio::spawn(async {
      |                    ^^^^^^^^^^^^ future created by async block is not `Send`
      |
      = help: within `impl Future&lt;Output = [async output]&gt;`, the trait `Send` is not implemented for `std::sync::MutexGuard&lt;'_, tokio::net::TcpStream&gt;`
  note: future is not `Send` as this value is used across an await
     --&gt; src\main.rs:20:31
      |
  16  |             let mut stream = common_stream.lock().unwrap();
      |                 ---------- has type `std::sync::MutexGuard&lt;'_, tokio::net::TcpStream&gt;` which is not `Send`
  ...
  20  |             stream.write(&amp;buf).await.unwrap();
      |                               ^^^^^^ await occurs here, with `mut stream` maybe used later
  21  |         }
      |         - `mut stream` is later dropped here
  note: required by a bound in `tokio::spawn`
     --&gt; .cargo\registry\src\mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd\tokio-1.17.0\src\task\spawn.rs:127:21
      |
  127 |         T: Future + Send + 'static,
      |                     ^^^^ required by this bound in `tokio::spawn`
  
  error: future cannot be sent between threads safely
     --&gt; src\main.rs:25:20
      |
  25  |     let handler2 = tokio::spawn(async {
      |                    ^^^^^^^^^^^^ future created by async block is not `Send`
      |
      = help: within `impl Future&lt;Output = [async output]&gt;`, the trait `Send` is not implemented for `std::sync::MutexGuard&lt;'_, tokio::net::TcpStream&gt;`
  note: future is not `Send` as this value is used across an await
     --&gt; src\main.rs:28:31
      |
  27  |             let mut stream = heartbeat_stream.lock().unwrap();
      |                 ---------- has type `std::sync::MutexGuard&lt;'_, tokio::net::TcpStream&gt;` which is not `Send`
  28  |             stream.write_u8(1).await.unwrap();
      |                               ^^^^^^ await occurs here, with `mut stream` maybe used later
  ...
  31  |         }
      |         - `mut stream` is later dropped here
  note: required by a bound in `tokio::spawn`
     --&gt; .cargo\registry\src\mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd\tokio-1.17.0\src\task\spawn.rs:127:21
      |
  127 |         T: Future + Send + 'static,
      |                     ^^^^ required by this bound in `tokio::spawn`
  </code></pre>
  <p>How can these errors be fixed or is there another way to achieve the same goal?</p>