this is my first question and I hope to post it right. I am trying to compile this simple program in which I use for the first time std::thread
#include <thread>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
bool isOdd (int i) { return ((i%2)==1); }
void countOdd(std::vector<int>::iterator first, std::vector<int>::iterator last, int& count)
{
count = count_if(first, last, isOdd);
}
int main()
{
std::vector<int> myvector;
for (int i=0; i<1000000; i++) myvector.push_back(i);
int count = 0;
std::thread(countOdd, myvector.begin(), myvector.end(), std::ref(count));
std::cout << count << std::endl;
return 0;
}
But I get the error:
g++ multithreadExample.cpp -o multithreadExample
/tmp/ccaJls0z.o: nella funzione "std::thread::thread<void (&)(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int&), __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, std::reference_wrapper<int> >(void (&)(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int&), __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&&, std::reference_wrapper<int>&&)":
multithreadExample.cpp:(.text._ZNSt6threadC2IRFvN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES7_RiEJS7_S7_St17reference_wrapperIiEEEEOT_DpOT0_[_ZNSt6threadC5IRFvN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES7_RiEJS7_S7_St17reference_wrapperIiEEEEOT_DpOT0_]+0x42): riferimento non definito a "pthread_create"
collect2: error: ld returned 1 exit status
I am working on Ubuntu 18.04 with
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Any guess about it? Many thanks in advance.