samedi 28 octobre 2017

g++ - Why do I have to pass "-pthread" option while using std::thread?

I'm just trying to understand a concept used by g++. Here my very simple std::thread application:

#include <iostream>
#include <thread>

void func() {
    std::cout << "Running..." <<  std::endl;
}

int main()
{
    std::thread t(func);
    t.join();
    return 0;
}

I'm able to compile it on macOs/Xcode 9.0 setup with following command:

g++ main.cpp -std=c++11

But I'm unable to compile it on Linux with the same command, as far as I know I have to pass -pthread option too. Otherwise it gives me following error:

gcc version 7.1.1 20170622 (Red Hat 7.1.1-3)

main.o: In function `std::thread::thread<void (&)()>(void (&)())':
/usr/include/c++/5/thread:137: undefined reference to `pthread_create'

I think it's illogical and I shouldn't even know that it's implementing the std::thread class via pthread. Why do I have to pass -pthread option and link against pthread library? Isn't C++11 supposed to abstract me away from platform specific details? Or do I have any other alternative libraries such as pthread that I can link against for my std::thread usage? Or should I report that as a bug?

Thanks.

Aucun commentaire:

Enregistrer un commentaire