<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - std::async with default policy moves their arguments twice if std::thread cannot be created"
   href="https://llvm.org/bugs/show_bug.cgi?id=28285">28285</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::async with default policy moves their arguments twice if std::thread cannot be created
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kariya_mitsuru@hotmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In the libc++'s current implementation of std::async with default policy (i.e.
async | deferred),
first, the argument function is launched in the context of a new thread
asynchronously (async).
If it was failed, the function is launched in the context of the current thread
later (deffered).

But if the first launch was failed (ex. std::thread creation trows an
exception), std::async's
arguments was already moved.
So, the arguments have no valid values when second launch is executing.


The sample code below simulates std::thread creation failure.

============================== sample code ==============================
#include <iostream>
#include <future>
#include <string>
#include <system_error>

struct S {
    S() : b(false) {}
    // to simulate thread creation failure, it throws
resource_unavailable_try_again when 'o' is first move target
    S(S&& o) : b(true) {
        if (!o.b) {
            o.b = true;
            throw
std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
        }
    }
    S(const S&) = delete;
    S& operator=(const S&) = delete;
    S&& operator=(S&&) = delete;
    bool b;
};

int main()
{
    auto f = std::async([](std::string&& s, S&&){ std::cout << "s = '" << s <<
'\'' << std::endl; }, std::string{"str"}, S{});
    f.get();
}
============================== sample code ==============================
============================== output ==============================
s = ''
============================== output ==============================
cf. <a href="http://melpon.org/wandbox/permlink/IZDRW8D7i0wifN91">http://melpon.org/wandbox/permlink/IZDRW8D7i0wifN91</a>


I think that it should output "s = 'str'" or should output nothing,
whether exception is thrown or not.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>