Contents

Threads, vectors and references in C++11 on OS X

Contents

I was trying to compile some C++ of the form

std::vector<std::thread> threads;
for (int i = 0; i<num_threads; ++i) {
   threads.push_back(std::thread(logisticTest, kmer_lines[i], samples);
}

with function prototype

void logisticTest(Kmer& k, const std::vector<Sample>& samples);

on OS X 10.10 with clang++ - Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)

First error message: no matching function for call to __invoke

Solution: add -std=c++11 -stdlib=libc++ to CXXFLAGS in Makefile (thanks to http://stackoverflow.com/questions/14115314/how-to-compile-c11-with-clang-3-2-on-osx-lion)

Second error message: attempt to use a deleted function __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::mov...

Solution: pass to thread by value rather than reference. e.g.

threads.push_back(std::thread(logisticTest, std::ref(kmer_lines[i]), std::cref(samples)));

(thanks to http://stackoverflow.com/questions/8299545/passing-arguments-to-thread-function)