0

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.

vaeVictis
  • 484
  • 1
  • 3
  • 13
  • 1
    Just add `-pthread` to the compilation command. – cigien Oct 14 '20 at 13:26
  • 1
    Compile with pthread e.g. g++ file.cpp -lpthread – Hikmat Farhat Oct 14 '20 at 13:26
  • 1
    compile by linking with pthread library (-lpthread) – Harry Oct 14 '20 at 13:27
  • the STL containers are not thread safe, you might run into nasty surprises if you pass the iterators like this, since the iterator can be invalidated for std::vector – zpasztor Oct 14 '20 at 13:28
  • 1
    there is a chance of main thread getting terminated before `countOdd` thread completes – Harry Oct 14 '20 at 13:31
  • Thanks everybody @zpasztor It's an adaptation of an example from "C++ Concurrency in Action". Could you please argue your answer maybe with an example? Thanks in advance. – vaeVictis Oct 14 '20 at 13:41
  • @Harry Of course, I did not joined nor detached the thread. I was posting this part of the code because I got an error compiling it. Thank you. – vaeVictis Oct 14 '20 at 13:43
  • https://stackoverflow.com/questions/6438086/iterator-invalidation-rules , iterator is like a pointer, if the vector changes, iterator may point to invalid space, your code will most likely run, it will give a result, just some garbage – zpasztor Oct 14 '20 at 14:12

0 Answers0