Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread in C++ in MacOS X

I'm trying to run some code using threads in standard C++ (installed with XCode) in MacOS X Mavericks. But I'm getting some errors. Here's a minimal working example:

#include <thread>
#include <iostream>

void run (int x) {
    std::cout<<".";
}

int main (int argc, char const *argv[])
{
    std::thread t(run);
}

The error I'm getting:

minimal.cpp:10:17: error: no matching constructor for initialization of 'std::thread'
std::thread t(run,0);
            ^ ~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:372:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments
  were provided
thread::thread(_Fp __f)
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:261:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
thread(const thread&);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:268:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
thread() _NOEXCEPT : __t_(0) {}
^
1 error generated.

I've been able to track the problem to my compiler defining _LIBCPP_HAS_NO_VARIADICS, which is defined because of

#if !(__has_feature(cxx_variadic_templates))
#define _LIBCPP_HAS_NO_VARIADICS
#endif

Any help would be appreciated.

Thank you!

like image 489
spalac24 Avatar asked Feb 26 '14 04:02

spalac24


People also ask

What is a thread on Mac?

Each process (application) in OS X or iOS is made up of one or more threads, each of which represents a single path of execution through the application's code. Every application starts with a single thread, which runs the application's main function.

Can we use thread in C?

Thread functions in C/C++ In a Unix/Linux operating system, the C/C++ languages provide the POSIX thread(pthread) standard API(Application program Interface) for all thread related functions. It allows us to create multiple threads for concurrent process flow.

Does MacOS have multithreading?

OS X is a fully multithread-aware operating system. The latest version of Java runs on it, and multithreaded java programs and applets are all fully functional, provided they dont require some OS-specific or hardware-specific resource. 1.6. 0_14 is most current.

What is thread in iOS?

Threads. A thread is a sequence of instructions that can be executed by a runtime. Each process has at least one thread. In iOS, the primary thread on which the process is started is commonly referred to as the main thread. This is the thread in which all UI elements are created and managed.


1 Answers

Thanks to pwny and PeterT, I figured out the error.

I just needed to compile with clang++ -std=c++11 minimal.cpp and it worked like a charm. I also needed a t.join() at the end to prevent an execution error to happen.

like image 82
spalac24 Avatar answered Oct 13 '22 21:10

spalac24