llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.11k stars 12.01k forks source link

std::promise/future deadlock #29724

Open llvmbot opened 8 years ago

llvmbot commented 8 years ago
Bugzilla Link 30376
Version unspecified
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @asl,@mclow

Extended Description

The bug happens with "Apple LLVM 7.0.0" (clang 7) on OSX 10.11.1 (El Capitan) with XCode 7.0.1. The program below will write out a series of numbers and after a short while hang, whereas it shouldn't. It runs flawlessly on Linux.

#include <iostream>
#include <thread>
#include <future>
#include <boost/asio.hpp>

using std::cout;
using std::endl;

static boost::asio::io_service io;

static void asio_thread()
{
    boost::asio::io_service::work work(io);
    cout << "STARTING ASIO" << endl;
    io.run();
    cout << "STOPPING ASIO" << endl;
}

static int poll()
{
  static int n;

  std::promise<int> promise;
  auto fut = promise.get_future();

  io.post([&promise]() {
    promise.set_value(n++);
  });

  return fut.get();
}

int main()
{
  std::thread thread(asio_thread);

  size_t n = 0;

  while (1)
    cout << "POLL: " << poll() << "\n";

  return 0;
}
llvmbot commented 8 years ago

To add more info: the bug seems to be fixed in Apple clang 7.3. The program runs indefinitely w/o deadlock as expected.

llvmbot commented 8 years ago

I didn't answer your question. Yes, it's related to that thread.

llvmbot commented 8 years ago

The only reason that the above program is "broken" ATM is that wording in the standard doesn't forbid set_value to do something additional after the atomic update and signaling of the shared state. I believe it is reasonable to expect that the use-case above works, especially in C++11 where you cannot move promise into a lambda.

Therefore I'm going to file a defect report to the committee.

mclow commented 8 years ago

Just checking - is this : https://www.reddit.com/r/cpp/comments/52j3fk/weird_problem_with_stdpromise_and_boost_asio/ ?

asl commented 8 years ago

Please either reproduce with mainline clang or report the problem with Apple-shipped tools to Apple bugtracker.

BillyONeal commented 9 months ago

The only reason that the above program is "broken" ATM is that wording in the standard doesn't forbid set_value to do something additional after the atomic update and signaling of the shared state. I believe it is reasonable to expect that the use-case above works, especially in C++11 where you cannot move promise into a lambda.

Therefore I'm going to file a defect report to the committee.

This is at least related to https://cplusplus.github.io/LWG/issue2530 ; I wrote a PR https://cplusplus.github.io/LWG/lwg-active.html#2532