C++11 has some notion of threads. For example, it defines a new storage specifier thread_local
, and specifies that for variables with this storage specifier, "there is a distinct object or reference per thread" [basic.stc.thread].
What is considered to be a "thread" for this purpose? Is it only threads created using the standard thread library (i.e. those represented by std::thread
objects)? What about threads created by other means (for example, by using pthreads directly on Linux)? What if I use a library that provides user-space threads - does each of those get its own copies of thread_local
objects (I don't really see how that could be implemented)?
If the answer is "it's implementation-defined what is considered to be a thread for purposes such as thread_local
", could someone give an example of how one well-known implementation defines this?
Multithreading in C++ C++ 11 did away with all that and gave us std::thread. The thread classes and related functions are defined in the thread header file. std::thread is the thread class that represents a single thread in C++.
Thread-based multitasking deals with the concurrent execution of pieces of the same program. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
If you need to execute the operation on a specific instance of the function object, you should use std::ref() from <functional> header to pass your function object by reference.
C++11 includes new smart pointer classes: shared_ptr and the recently-added unique_ptr. Both are compatible with other Standard Library components, so you can safely store these smart pointers in standard containers and manipulate them with standard algorithms.
Only components from the thread support library count because of these quotes, or main
which the standard states runs in its own thread of execution.
1 The following subclauses describe components to create and manage threads (1.10), perform mutual exclusion, and communicate conditions and values between threads, as summarized in Table 148.
The link to 1.10 implies that the threads being spoken about are these.
1 A thread of execution (also known as a thread) is a single flow of control within a program, including the initial ...
Therefore it seems to me threads only refer to the stdlib threads (meaning std::thread and anything the thread support library does internally). Of course thread_local
in many cases could end up working with the native threads (especially when you consider on a specific system you don't usually have more than one choice for implementing threads) but as far as I can tell the standard makes no guarantee.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With