为使用迭代器作为参数的 lambda 函数创建打包任务

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

(在 Ubuntu 18.04 上使用 C++17 和 g++ 7.5.0)
下面的 dp1 工作正常,但 dp2 导致以下错误:

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;
}


加载程序似乎缺少 pthreads 库。尝试添加类似 g++ -o myprog myprog.cpp -lpthread

的库