I'm having a weird problem (at least in my eyes) with C++. I've created a thread pool class, and in the main function I just pushes one element to the task queue. This gives me an error saying "undefined symbols". When the specific line is commented out, the program compiles.
Here is the push
method in question:
template<class F, class ...Args>
void concurrency::thread_pool::push(F&& f, Args&&... args) {
if (stop) {
throw std::runtime_error("push on stopped thread_pool");
}
std::function<void()> func = std::bind(f, args...);
lockGuard l(mutex);
bool should_wake = queue.empty();
queue.push(func);
if (should_wake) {
cond.notify_one();
}
}
And here's the main function:
void hello_world() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
pool = new concurrency::thread_pool(5);
pool->push(hello_world); // <-- Compile error!
}
This is the command:
david$ g++ -g -Wall -std=c++0x -I ./ *.cpp
Undefined symbols for architecture x86_64:
"void concurrency::thread_pool::push<void (&)()>(void (&)())", referenced from:
_main in ccuKcG16.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
And my g++ version:
davids-mbp:cpp-thread-pool david$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc47/4.7.3/libexec/gcc/x86_64-apple-darwin13.0.2/4.7.3/lto-wrapper
Target: x86_64-apple-darwin13.0.2
Configured with: ../configure --build=x86_64-apple-darwin13.0.2 --prefix=/usr/local/Cellar/gcc47 /4.7.3 --enable-languages=c,c++,objc,obj-c++ --program-suffix=-4.7 --with-gmp=/usr/local/opt/gmp4 --with-mpfr=/usr/local/opt/mpfr2 --with-mpc=/usr/local/opt/libmpc08 --with-ppl=/usr/local/opt/ppl011 --with-cloog=/usr/local/opt/cloog-ppl015 --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --enable-plugin --disable-nls --disable-multilib
Thread model: posix
gcc version 4.7.3 (GCC)
Update
- thread_pool.hpp: http://pastebin.com/62SDD0ZY
- thread_pool.cpp: http://pastebin.com/nqz57HS9