c++packaged-task

Creating a packaged task for a lambda function with iterators as arguments


(using C++17 and g++ 7.5.0 on Ubuntu 18.04)
dp1 below works fine, but dp2 results in the following error:

undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

#include <vector>
#include <iostream>
#include <random>
#include <thread>
#include <future>

int main(void)
  {

  //define lambda function
  auto f = [](auto it1, auto it2, auto it3){
    return std::inner_product(it1,it2,it3,0.0);
  };

  std::vector<int> v1 {3,5,10};
  std::vector<int> v2 {4,7,2};

  auto dp1 = std::inner_product(std::begin(v1), std::end(v1), std::begin(v2), 0.0);
  std::cout<<"dp1: "<<dp1<<"\n"; //this part works fine

  //set up packaged task
  std::packaged_task<int (std::vector<int>::iterator,std::vector<int>::iterator,std::vector<int>::iterator)> pt1 {f};
  auto f1=pt1.get_future(); //get future
  std::thread t1 {std::move(pt1),std::begin(v1),std::end(v1),std::begin(v2)}; //move pt to thread

  auto dp2= f1.get();
  std::cout<<"dp2: "<<dp2<<"\n";
  t1.join();


  return 0;
}



Solution

  • Looks like the loader is missing pthreads library. Try adding the library with something like g++ -o myprog myprog.cpp -lpthread