title: Broken promise, having trouble figuring it out (C++)
body: <p>The error message I'm getting:</p>
  <pre><code>Unhandled exception at 0x7712A9F2 in eye_tracking.exe: Microsoft C++ exception: std::future_error at memory location 0x010FEA50.
  </code></pre>
  <p>Code snippet of where I fork and join:</p>
  <pre><code>//CONCURRENCE
  std::vector&lt;costGrad*&gt; threadGrads;
  std::vector&lt;std::thread&gt; threads;
  std::vector&lt;std::future&lt;costGrad*&gt;&gt; ftr(maxThreads);
  
  for (int i = 0; i &lt; maxThreads; i++)    //Creating threads
  {
      int start = floor(xValsB.rows() / (double)maxThreads * i);
      int end = floor(xValsB.rows() / (double)maxThreads * (i+1));
      int length = end-start;
      std::promise&lt;costGrad*&gt; prms;
      ftr[i] = prms.get_future();
      threads.push_back(std::thread([&amp;]() 
      {
          costThread(std::move(prms), params, xValsB.block(start, 0, length, xValsB.cols()), yVals.block(start, 0, length, yVals.cols()), lambda, m); 
      }));
  }
  
  for (int i = 0; i &lt; maxThreads; i++)    //Collecting future
      threadGrads.push_back(ftr[i].get()); &lt;-------I THINK THIS IS WHERE I'M MESSING UP
  
  for (int i = 0; i &lt; maxThreads; i++)    //Joining threads
      threads[i].join();
  </code></pre>
  <p>Following is the <code>costThread</code> function:</p>
  <pre><code>void costThread(std::promise&lt;costGrad*&gt; &amp;&amp; pmrs, 
                  const std::vector&lt;Eigen::MatrixXd&gt;&amp; params, 
                  const Eigen::MatrixXd&amp; xValsB, 
                  const Eigen::MatrixXd&amp; yVals, 
                  const double lambda, 
                  const int m) 
  {
      try
      {
          costGrad* temp = new costGrad;      //&quot;Cost / Gradient&quot; struct to be returned at end
  
          temp-&gt;forw = 0;
          temp-&gt;back = 0;
  
          std::vector&lt;Eigen::MatrixXd&gt; matA;          //Contains the activation values including bias, first entry will be xVals
          std::vector&lt;Eigen::MatrixXd&gt; matAb;         //Contains the activation values excluding bias, first entry will be xVals
          std::vector&lt;Eigen::MatrixXd&gt; matZ;          //Contains the activation values prior to sigmoid
          std::vector&lt;Eigen::MatrixXd&gt; paramTrunc;    //Contains the parameters exluding bias terms
  
          clock_t t1, t2, t3;
          t1 = clock();
  
          //FORWARD PROPAGATION PREP
  
          Eigen::MatrixXd xVals = Eigen::MatrixXd::Constant(xValsB.rows(), xValsB.cols() + 1, 1); //Add bias units onto xVal
          xVals.block(0, 1, xValsB.rows(), xValsB.cols()) = xValsB;
  
          matA.push_back(xVals);
          matAb.push_back(xValsB);
  
          //FORWARD PROPAGATION
  
          for (int i = 0; i &lt; params.size(); i++)
          {
              Eigen::MatrixXd paramTemp = params[i].block(0, 1, params[i].rows(), params[i].cols() - 1);      //Setting up paramTrunc
  
              paramTrunc.push_back(paramTemp);
  
              matZ.push_back(matA.back() * params[i].transpose());
              matAb.push_back(sigmoid(matZ.back()));
  
  
              Eigen::MatrixXd tempA = Eigen::MatrixXd::Constant(matAb.back().rows(), matAb.back().cols() + 1, 1); //Add bias units
              tempA.block(0, 1, matAb.back().rows(), matAb.back().cols()) = matAb.back();
  
              matA.push_back(tempA);
          }
  
          t2 = clock();
  
          //COST CALCULATION
  
          temp-&gt;J = (yVals.array()*(0 - log(matAb.back().array())) - (1 - yVals.array())*log(1 - matAb.back().array())).sum() / m;
  
          //BACK PROPAGATION
  
          std::vector&lt;Eigen::MatrixXd&gt; del;
          std::vector&lt;Eigen::MatrixXd&gt; grad;
  
          del.push_back(matAb.back() - yVals);
  
          for (int i = 0; i &lt; params.size() - 1; i++)
          {
              del.push_back((del.back() * paramTrunc[paramTrunc.size() - 1 - i]).array() * sigmoidGrad(matZ[matZ.size() - 2 - i]).array());
          }
          for (int i = 0; i &lt; params.size(); i++)
          {
              grad.push_back(del.back().transpose() * matA[i] / m);
              del.pop_back();
          }
          for (int i = 0; i &lt; params.size(); i++)
          {
              int rws = grad[i].rows();
              int cls = grad[i].cols() - 1;
              Eigen::MatrixXd tmp = grad[i].block(0, 1, rws, cls);
              grad[i].block(0, 1, rws, cls) = tmp.array() + lambda / m*paramTrunc[i].array();
          }
  
          temp-&gt;grad = grad;
  
          t3 = clock();
  
          temp-&gt;forw = ((float)t2 - (float)t1) / 1000;
          temp-&gt;back = ((float)t3 - (float)t2) / 1000;
  
          pmrs.set_value(temp);
      }
  
      catch (...)
      {
          pmrs.set_exception(std::current_exception());
      }
      //return temp;
  }
  </code></pre>
  <h2>EDIT:</h2>
  <p>Figured out the exception is a broken promise. I'm still having problems understanding what I'm getting wrong here. At the end of <code>costThread()</code> I use</p>
  <pre><code>pmrs.set_value(temp);
  </code></pre>
  <p>And I expect the following to get <code>temp</code>:</p>
  <pre><code>for (int i = 0; i &lt; maxThreads; i++)    //Collecting future
      threadGrads.push_back(ftr[i].get());
  </code></pre>
  <p>But somehow I'm getting it all wrong.</p>