I have compiled a c++ code using g++ -std=c++11 -o main main.cpp -pthread
and it compiled fine however if I compile the same code using gcc -std=c++11 -o main main.cpp -pthread
it does not compile and throws error. The program uses threading which properly taken care of using -pthread
option while compiling. For the reference I am attaching the code below. Any help is highly appreciated.
#include <iostream>
#include <thread>
class foo
{
public:
void bar(int loop_num)
{
for (int i = 0; i < loop_num; ++i) {
std::cout << "Thread executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int n = 0;
};
int main()
{
int n = 0;
foo f;
std::thread t1(&foo::bar, &f, 5);
t1.join();
}