title: Calling a member function in a new thread
body: <p>This is a class:</p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;vector&gt;
  
  class A {
    private:
      void render(int, std::vector&lt;std::vector&lt;int&gt;&gt;&amp;) {
          //do stuff
      };
  
      void renderSegment() {
          int var1 = 1;
          std::vector&lt;std::vector&lt;int&gt;&gt; var2;
  
          std::thread t(????);
          t.join();
      };
  };
  </code></pre>
  
  <p>Here are some attempts:</p>
  
  <pre><code>std::thread t([this, var1, &amp;var2] {renderSegment(var1, var2);});
  std::thread t(renderSegment, var1, std::ref(var2));
  std::thread t(&amp;A::renderSegment, var1, std::ref(var2));
  std::thread t(&amp;renderSegment, this, var1, std::ref(var2));
  </code></pre>
  
  <p>I want to call <code>render(var1, var2)</code> in the function <code>renderSegment()</code>. I've look at a couple of questons, like this</p>
  
  <p><a href="https://stackoverflow.com/questions/10673585/start-thread-with-member-function">Start thread with member function</a></p>
  
  <p>and this</p>
  
  <p><a href="https://stackoverflow.com/questions/29754438/c11-multithreading-with-class-member-function">C++11 multithreading with class member function</a></p>
  
  <p>but nothing seems to work. Even when it looks like it should work, the compiler tells me that that I'm attempting to reference a deleted function <code>std::thread::thread(const std::thread &amp;)</code>.</p>
  
  <p>I'm using C++17 and MVSC, for better or for worse. I haven't enabled any kind of special flags, but I was unable to find any that would explicitly enable multithreading in the program itself (there is one for multithreaded compilation).</p>
  
  <p>How do I make it work?</p>
  
  <p>EDIT: Here is longer, but real code:</p>
  
  <p>Renderer.h</p>
  
  <pre><code>#include &lt;glm/vec2.hpp&gt;
  #include &lt;glm/vec3.hpp&gt;
  
  #include &lt;complex&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  
  class State;
  
  class Renderer {
  public:
      void renderScene();
      Renderer(State&amp; state);
  
  private:
      State&amp; m_state;
  
      void renderMandelbrot(int power);
      double mandelbrot(std::complex&lt;double&gt; c, int power);
      void renderSegment(
          int segmentStart,
          int segmentSize,
          int power,
          std::vector&lt;std::vector&lt;glm::vec3&gt;&gt;&amp; colours);
  };
  </code></pre>
  
  <p>Renderer.cpp</p>
  
  <pre><code>#include "Renderer.h"
  
  #include "State.h"
  
  #include &lt;algorithm&gt;
  #include &lt;thread&gt;
  #include &lt;time.h&gt;
  
  void Renderer::renderScene() {
      renderMandelbrot(m_state.m_power);
  }
  
  Renderer::Renderer(State&amp; state) :
      m_state(state) {
  
      std::srand(time(NULL));
      m_state.m_threadCount = std::thread::hardware_concurrency();
  }
  
  void Renderer::renderMandelbrot(int power) {
      std::vector&lt;std::vector&lt;glm::vec3&gt;&gt; colours;
      colours.resize(m_state.m_height);
      for (int i = 0; i &lt; m_state.m_height; ++i) {
          colours[i].resize(m_state.m_width);
      }
  
      int segmentSize = m_state.m_height / m_state.m_threadCount;
      int segmentStart = 0;
  
      std::vector&lt;std::thread&gt; threads;
  
      for (int k = 0; k &lt; m_state.m_threadCount - 1; ++k, segmentStart += segmentSize) {
          std::thread t = std::thread(&amp;Renderer::renderMandelbrot, this,
              segmentStart,
              segmentStart + segmentSize,
              power,
              std::ref(colours));
          threads.push_back(t);
      }
      std::thread t = std::thread(&amp;Renderer::renderMandelbrot, this,
              segmentStart,
              m_state.m_height,
              power,
              std::ref(colours));
          threads.push_back(t);
  
      for (auto&amp; thread : threads) {
          thread.join();
      }
  }
  
  void Renderer::renderSegment(
      int segmentStart,
      int segmentEnd,
      int power,
      std::vector&lt;std::vector&lt;glm::vec3&gt;&gt;&amp; colours) {
  
      for (int j = segmentStart; j &lt; segmentEnd; ++j) {
          for (int i = 0; i &lt; m_state.m_width; ++i) {
              colours[j][i] = glm::vec3(1.0f, 1.0f, 1.0f);
          }
      }
  }
  </code></pre>
  
  <p>State is pretty much just a bunch of values, so I omitted the code for it.</p>