Hello all,
I have a few questions on using threads.
1st Question
Whenever I write some code using threads and compile it with gcc (v 4.9), I have to include a linker switch like this:
Or else I get the error "undefined reference to 'pthread_create'":
Code:
// Probably not great code, just want to demonstrate the problem
#include <iostream>
#include <thread>
#include <mutex>
std::mutex cout_lock;
int main () {
int num = 0;
auto countToTen = [] (int& n)->void {
for (int i = 0; i < 10; i++) {
n += 1;
std::lock_guard<std::mutex> lock(cout_lock);
std::cout << i << "\n";
}
};
std::thread t1(countToTen, std::ref(num));
cout_lock.lock();
std::cout << "Waiting to finish.\n";
cout_lock.unlock();
t1.join();
std::cout << "Finished counting, final value of iterator: " << num << "\n";
return 0;
}
// undefined reference to 'pthread_create'
I was wondering if this error (without the linking command) was expected behavior? Are threads just a special case in this regard (I can't remember needing to link for vanilla std namespace classes before), or is there something I'm potentially missing?
2nd Question
Since I'll also be using some boost libraries (mostly asio, maybe bind), I was wondering what the right thing to do in regards to which version of similar classes to use, in those cases where both libraries provide classes for the same thing?
Various tutorials seem to indicate you should always use boost classes for maximal compatibility with boost, but they could be biased, so I thought I would ask. (I know it's general though).
Thanks!!!