packaged function object runtime exception


I am trying to test a packaged task, using std::packaged_task.
I am unsure why this code generates a runtime error though.

Can anybody point me to working examples of std::packaged_task ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <future>
#include <iostream>

int factorial(int N) {
  int res = 1;
  for (int i=N; i>1; i--) {
    res *= i;
  }
  return res;
}

int main() {

  // setup task, bind arg to function.  
  std::packaged_task<int(int)> t(std::bind(factorial, 5));

  // ..
  
  t(5); // Execute
  int x = t.get_future().get(); // return value
  std::cout << x << std::endl;

  return 0;
}


I am using gcc 6.3 on Ubuntu 17.04.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <future>
#include <iostream>
#include <functional> // required

int factorial( int n ) { return n < 2 ? 1 : n * factorial(n-1) ; }

int main() {

  // unary task (do not bind any args): invoke passing one integer argument
  std::packaged_task< int(int) > unary_task(factorial) ; 

  // nullary task (bind the integer argument): invoke without passing any arguments
  std::packaged_task< int() > nullary_task( std::bind(factorial,5) ) ; // bind 

  unary_task(5) ; // unary: pass one argument
  nullary_task() ; // nullary: no arguments

  const int x = unary_task.get_future().get() ;
  const int y = nullary_task.get_future().get() ;
  std::cout << x << ' ' << y << '\n' ;
}

http://coliru.stacked-crooked.com/a/c998f718a57853f5
http://rextester.com/SXA23640


thanks for replying JLBorges,

I am unsure why, but the same runtime error is generated for the code you posted as the first post.

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


But when I run it on rextester.com , then it runs as expected.

I am using :
1
2
$ g++ --version
g++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
To use standard C++ threads with the GNU compiler/library, we need to specify the compiler option -pthread

> echo && echo clang++ && clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
> echo && echo 'g++ with -pthread' && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out
> echo && echo 'g++ without -pthread' && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out

clang++
120 120

g++ with -pthread
120 120

g++ without -pthread
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1

http://coliru.stacked-crooked.com/a/eb1de642fc6b8273

thanks,
I was linking as follows:
g++ -g -Wall -o "%e" "%f" -L /usr/lib/x86_64-linux-gnu -lpthread -lboost_system

I've modified to the following:
g++ -g -Wall -o "%e" "%f" -L /usr/lib/x86_64-linux-gnu -lpthread -pthread -lboost_system

and no runtime errors!

thanks,
I was linking as follows:
g++ -g -Wall -o "%e" "%f" -L /usr/lib/x86_64-linux-gnu -lpthread -lboost_system

I've modified to the following:
g++ -g -Wall -o "%e" "%f" -L /usr/lib/x86_64-linux-gnu -lpthread -pthread -lboost_system

and no runtime errors!
Topic archived. No new replies allowed.