ozgur
ozgur

Reputation: 2719

Is c++11 thread platform independent?

I searched for lots of questions and answers, but I really couldn't figure out this issue. Yesterday I have tried C++11 thread on Windows in a Visual C++ project and it works fine.

Does that mean we can use C++11 threads on every platform that has a compiler with C++11 support? Are there any reasons not to use this thread instead of pthread or Windows thread (depending on the platform)?

Upvotes: 4

Views: 3836

Answers (4)

HelloWorld
HelloWorld

Reputation: 1863

The C++ 11 thread library still uses threads from the OS and relies on them but it is abstracted in a good way so you will experience almost no differences. The behavior is different only in the detail and you will almost not notice them (only on edge cases and/or on failure). There might be still some platforms out there which don't support everything from std::thread (even in 2015, e.g. on some specific / exotic mobile platforms).

From the C++ Standard:

30 Thread support library

Some functions described in this Clause are specified to throw exceptions of type system_error (19.5.6). Such exceptions shall be thrown if any of the function’s error conditions is detected or a call to an operating system or other underlying API results in an error that prevents the library function from meeting its specifications. Failure to allocate storage shall be reported as described in 17.6.5.12.

Upvotes: 5

user2622016
user2622016

Reputation: 6423

Yes, it is platform independent. It can be (and often is) implemented as wrapper for pthreads, so basically it can be pthreads with different API.

Upvotes: 1

ventsyv
ventsyv

Reputation: 3532

It really depends how well the compiler supports it. I would imagine GCC / VS /Intel have pretty good support by now but then if you are targeting some exotic platform that might not necessarily be the case.

That's been the case with the STL for a while now - it's mostly portable, but it really depends on the implementation. Just because it's in the standard does not mean it will magically work, even though the chance of it working is much higher than if it was not in the standard.

There will still be other implementations - pthread, boost, whatever. Which one you use depends on your personal preferences, your requirements, etc, etc.

Upvotes: 1

Open Kastle
Open Kastle

Reputation: 427

C++11's thread mechanisms are intended to be cross-platform like any other feature of standard C++. From my understanding, all major compilers intend to support the C++ standard to the best of their ability which means supporting the threading library.

Upvotes: 2

Related Questions