[LLVMbugs] [Bug 14934] New: <future> std::async possible deadlock

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Jan 12 11:35:20 PST 2013


             Bug #: 14934
           Summary: <future> std::async possible deadlock
           Product: libc++
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
        AssignedTo: hhinnant at apple.com
        ReportedBy: zhouyan1014 at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

I have found the problem on SVN version of Clang + Libc++ on Ubuntu 12.10,
and Clang + Libc++ on Mac OS X 10.8.2 with the latest Xcode.

Sometimes, when I spawn a work through async, the program halt forever. The
trouble is that the problem is difficult to reproduce and debug does not help
much. One of the reason I suspect there is some deadlock or similar issue is
that the problem seems to be very time sensitive.

However, I put together a program that may reproduce the problem,

// test.cpp
#include <cstdlib>
#include <future>
#include <iostream>
#include <thread>
#include <vector>

void do_nothing () {}

int main (int argc, const char **argv)
    if (argc < 3) {
            << "Usage: test <number of trials> <number of threads>"
            << std::endl;
        return -1;

    const int K = std::atoi(argv[1]);
    const int T = std::atoi(argv[2]);

    for (int k = 0; k != K; ++k) {
        std::cout << "Trial " << k << "...";
        std::vector<std::future<void> > tg;
        for (int t = 0; t != T; ++t)
        for (int t = 0; t != T; ++t)
        std::cout << "done" << std::endl;

    return 0;

The program accept two numbers, one is the number of trials, and the other is
how may async will be used in each trial. For each trial, a group of
std::future is obtained by calling std::async, which was called with an empty
function that does nothing. And then we wait and get the results, which is also

I compiled the program as,

clang++ -std=c++11 -stdlib=libc++ -g -O2 -o test test.cpp

(others configurations was tried, such as no debug info, or no optimization,
but I don't think that shall matter, since this is problem about correctness)

Normally, the program shall output 

./test 10 4
Trial 0...done
Trial 1...done
Trial 2...done
Trial 3...done
Trial 4...done
Trial 5...done
Trial 6...done
Trial 7...done
Trial 8...done
Trial 9...done

But if I increase the first number, say 10000, the screen start to scroll
rapidly (of course), but sooner or later, one of the trial halt there forever.

I am not quite sure exactly what bug is there. Or I oversight some very silly
problem in my own code. I tried the same program with MSVC 11 as well as GCC
4.7, no problem happened

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list