The advantage of c++11 threads

Viewed 1513

Threads are added to C++11 language

Then I am wondering what is the difference, advantages and impact?

If this code is by c++03

#include <iostream>
#include <pthread.h>

void *call_from_thread(void *)
{
    std::cout << "Launched by thread" << std::endl;
    return NULL;
}

int main()
{
    pthread_t t;

    pthread_create(&t, NULL, call_from_thread, NULL);

    pthread_join(t, NULL);
    return 0;
}

and this one by c++11

#include <iostream>
#include <thread>

void call_from_thread()
{
    std::cout << "Hello, World" << std::endl;
}

int main()
{
    std::thread t1(call_from_thread);
    t1.join();
    return 0;
}

Then, I see no fundamental advantage.

Also, when it is said a part of language, I am confused about that as I see no new keyword or no new syntax. I just see a new standard library. Is it beyond that? and is this just a paraphrase of pthread?

3 Answers

Besides being much more portable, C++11 threads also provides other benefits:

  • allows passing arguments (and more than one) to the thread handler in a type safe way. pthread_create passes a single void*, whereas with std::thread you get compile time errors if something is wrong instead of runtime errors
  • the thread handler can be a lambda
  • a std::thread is an object, not a pointer, which makes managing object lifetimes easier, and reduces risk of dangling pointers, especially if combined with std::unique_ptr or std::shared_ptr if pointer juggling is even needed.

Those are the immediate benefits that come to mind.

As for standard library vs language spec: they are both part of the same standard, so they are both considered "C++11". Note that std::thread can not be implemented in C++03, since move semantics is new in C++11 and std::thread implements move.

The primary advantage of C++ thread library is portability. Like many other C++ standard library facilities platform-dependent libraries like pthreads or Win32API provide more control over your threads comparing to C++ thread library. For instance on Windows Win32 API thread library allows you to set thread stack size you can't do with C++ thread library without using platform dependent code. API functions like TerminateThread allows developers to terminate their running threads (a very dangerous operation) or setting thread priority using function SetThreadPriority.

But, using C++ thread library makes your code platform independent. And it's not just about class thread. There are other facilities like mutexes, conditional variables, locks that have been standardized so every C++ implementation is supposed to implement them to comply with C++ standard.

So, using C++ thread library is always a trade-off. You are losing some degree of control over threads but your code is being portable. And if you really need some low level feature, you can use std::thread::native_handle that allows you to mix standard and platform dependent code.

This link std::thread::native_handle provides a nice example how to mix class thread and pthread library.

It sounds funny in 2019, but one small disadvantage of std::thread (since you asked about it) is ~130kb of code added to your binary.

Related