0

I am unsure why below code is throwing a runtime exception when run in online compiler here. I am trying to execute a function via packaged task

#include <iostream>
#include <future>

int display(int i)
{
    std::cout << "In display";
    return i + 1;
}

int main()
{
   std::packaged_task<int(int)> pt(display);
   auto fut = pt.get_future();
   pt(5);
   std::cout << "\n PT returns " << fut.get();
   return 0;
}

The error which i am getting is

/tmp/RucY1KsPrS.o
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted
Helena
  • 444
  • 2
  • 15
  • 1
    Compile your app with `-pthread`. For more [details](https://stackoverflow.com/questions/41790363/c-core-dump-with-packaged-task/41790617) – rafix07 Feb 08 '22 at 15:41
  • @rafix07 - I am using standard c++. Why should i link with pthread ? – Helena Feb 08 '22 at 15:50
  • @Helena Because you don't want a system error here. The compiler you are using, by default, does not include threading support. If you want threading support, you have to ask for it by adding `-pthread`. This is probably for reverse compatibility reasons. Strange, but that is life! – Yakk - Adam Nevraumont Feb 09 '22 at 14:50
  • @Yakk-AdamNevraumont - Is there any way to find out in advance if compiler being used has threading support? This will be better than wasting time later debugging. – Helena Feb 10 '22 at 05:38
  • @helena Compiler documentation usually covers what flags are and what they mean. There are now 3 major C++ compilers; learn all 3? – Yakk - Adam Nevraumont Feb 10 '22 at 13:54
  • @Yakk-AdamNevraumont - as mentioned in my problem that I am using an online c++ compiler. I don't want to study flags. I wanted to know how to determine if specific compiler has standard c++ threading support or not . How to find this in the online compiler link I posted? – Helena Feb 10 '22 at 17:08
  • @Helena That sounds like tech support for that website problem? A good online C++ compiler will tell you what compiler it is using. That one doesn't. Imagine you have an online Java compiler that produces an error if your variables have the letter 'a' in them. There is no standard way to detect this, because it is a problem of your online compiler, not Java. programiz did a poor job of providing an online C++ compiler. /shrug. – Yakk - Adam Nevraumont Feb 10 '22 at 20:38
  • @Yakk-AdamNevraumont : so the online compiler doesnt have std::thread support but then how using -pthread is able to resolve thread under namespace std? I don't think pthread defines std::thread. – Helena Feb 10 '22 at 21:48
  • @Helena Again, this is a tech support problem for that website. They have a broken C++ compiler. I can tell you how *they* could fix their broken C++ compiler, but that doesn't help you (and that involves passing `-pthreads`, or the equivalent, to their compiler). Their interface to their C++ compiler sucks, so it looks like you cannot fix it; other online C++ compilers give you access to the command line. I'm getting this sucks, but there is nothing I can do about a broken website, even if it has a C++ compiler on it. – Yakk - Adam Nevraumont Feb 11 '22 at 03:14

0 Answers0