I'm having trouble multithreading in C++. Here's my code:
#include <iostream>
#include <thread>
bool stop = false;
void loop() {
while (!stop) {
// Do something...
}
}
int main() {
std::thread t(loop);
std::cin.get();
stop = true;
t.join();
std::cout << "\nPress any key to continue";
std::cin.get();
return 0;
}
What I'm trying to accomplish is to break out of the loop in loop()
as soon as a key is pressed. When I try to compile, it throws an error:
$ sudo g++ main.cpp
/usr/bin/ld: /tmp/ccZOsb6T.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x20): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
Am I doing something wrong? Thanks for any help.