[llvm-bugs] [Bug 28285] New: std::async with default policy moves their arguments twice if std::thread cannot be created

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jun 23 22:46:43 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28285

            Bug ID: 28285
           Summary: std::async with default policy moves their arguments
                    twice if std::thread cannot be created
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: kariya_mitsuru at hotmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

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. http://melpon.org/wandbox/permlink/IZDRW8D7i0wifN91


I think that it should output "s = 'str'" or should output nothing,
whether exception is thrown or not.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160624/0d4ecb8c/attachment.html>


More information about the llvm-bugs mailing list