Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode std::thread C++

For a small project for school I need to create a simple client / server construction which will run on a router (with openWRT) and I am trying to do something with threads in this application.

My C++ skills are very limited, so I found this on the internet as an basic example.

#include <thread>
#include <iostream>

void doSomeWork( void )
{
    std::cout << "hello from thread..." << std::endl;
    return;
}

int main( int argc, char *argv[] )
{
    std::thread t( doSomeWork );
    t.join();
    return 0;
}

When I try to run this in Xcode (4.5.2) I get the following error:

Attempt to use an deleted function

And it shows some code of:

__threaad_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

I think I need to do something with 'build settings' or 'link library' or something? But I am not quite sure what to do exactly. I thought I might need to set the following settings (which i found here)

  • In the Build Settings tab for your project, scroll down to "Apple LLVM Compiler 4.1 - Language"
  • Set the setting "C++ Language Dialect" to "C++11 [-std=c++11]"
  • Set the setting "C++ Standard Library" to "libc++ (LLVM standard C++ library with C++11 support)"

But those settings where already set.

Is there any flag / library or something I am missing?

like image 358
Matthijn Avatar asked Dec 23 '12 15:12

Matthijn


1 Answers

Use G++ instead of LLVM in XCode. Don't forget to link thread libs (-lpthread - or -pthread, -lrt) in compiler build settings. And count with the differences of thread behaviour across Win/Mac/Linux OS (despite it's POSIX)

like image 147
bartimar Avatar answered Sep 19 '22 12:09

bartimar