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

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.

std::reference_wrapper: 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)

Advertisements
2 comments
  1. Booooo. You should be using GCC, the GNU Compiler Collection.

    • jofunu6 said:

      Ha, true!
      I will be eventually, but I need to get it to work on both in the end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s