title: Create new instance of std::thread/std::jthread on every read call
body: <p>I am developing a serial port program using boost::asio.<br>
  In synchronous mode I create a thread every time read_sync function is called. All reading related operation are carried in this thread (implementation is in read_sync_impl function). <br>
  On <code>close_port</code> or <code>stop_read</code> function reading operation is stopped. <br>
  This stopped reading operation can be restarted by calling the <code>read_sync</code> function again. <br>
  <code>read_sync</code> function will never be called successively without calling <code>close_port</code> or <code>stop_read</code>  function in between.</p>
  <p>I wish to know how to implement a class wide <code>std::jthread</code> along with proper destructor when I call my read_sync function. In languages like Kotlin or Dart the garbage-collector takes care of this. What is C++ implementation of this.</p>
  <pre><code>bool SerialPort::read_sync(std::uint32_t read_length, std::int32_t read_timeout)
  {
      this-&gt;thread_sync_read = std::jthread(&amp;SerialPort::read_sync_impl, this);
      return true;
  }
  
  bool SerialPort::read_sync_impl(const std::stop_token&amp; st)
  {
      while(true)
      {
          ...
          if (st.stop_requested())
          {
              PLOG_INFO &lt;&lt; &quot;Stop Requested. Exiting thread.&quot;;
              break;
          }
      }
  }
  
  bool SerialPort::close_port(void)
  {
      this-&gt;thread_sync_read-&gt;request_stop();
      this-&gt;thread_sync_read-&gt;join();
      this-&gt;port.close();
      return this-&gt;port.is_open();
  }
  
  class SerialPort
  {
  public :
      std::jthread *thread_sync_read = nullptr;
      ...
  }
  </code></pre>
  <hr />
  <p>Actual Code</p>
  <pre><code>bool SerialPort::read_sync(std::uint32_t read_length, std::int32_t read_timeout)
  {
      try
      {
          if (read_timeout not_eq ignore_read_timeout)
              this-&gt;read_timeout = read_timeout;//If read_timeout is not set to ignore_read_timeout, update the read_timeout else use old read_timeout
          if (this-&gt;thread_sync_read.joinable())
              return false; // Thread is already running
          thread_sync_read = std::jthread(&amp;SerialPort::read_sync_impl, this);
          return true;
      }
      catch (const std::exception&amp; ex)
      {
          PLOG_ERROR &lt;&lt; ex.what();
          return false;
      }
  }
  
  void SerialPort::read_sync_impl(const std::stop_token&amp; st)
  {
      try
      {
          while (true)
          {
              if (st.stop_requested())
              {
                  PLOG_INFO &lt;&lt; &quot;Stop Requested in SerialPort::read_sync_impl. Exiting thread.&quot;;
                  break;
              }
          }
      }
      catch (const std::exception&amp; ex)
      {
          PLOG_ERROR &lt;&lt; ex.what();
      }
  }
  
  class SerialPort
  {
      std::jthread                    thread_sync_read;
      SerialPort() : io(), port(io), thread_sync_read()
      {
          read_buffer.fill(std::byte(0));
          write_buffer.fill(std::byte(0));
      }
  }
  </code></pre>