0

I'm trying to wrap a packaged_task in a lambda in order to stock them inside a container. I wrote a test code below for simulating the wrapping and calling the lambda function. My code is as follows:

int test()
{
    return 10;
}

int main()
{
    auto task = std::make_shared<std::packaged_task<int()>>(test);
    auto result = task->get_future();
    auto wrapper = [=]() { (*task)(); };
    wrapper();
}

The program got aborted with the following exception:

terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 Aborted (core dumped)

Could someone explain me why the exception is thrown?

Minee
  • 408
  • 5
  • 12

1 Answers1

3

std::packaged_task::operator() indirectly uses std::call_once, which, according to this link, requires pthread library to operate, otherwise it throws std::system_error. So to get rid of this exception, you need to build with -lpthread. Sounds weird, but worked for me.

grungegurunge
  • 841
  • 7
  • 13