Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does C++11 consider to be a "thread"?

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?

like image 489
HighCommander4 Avatar asked Nov 04 '13 00:11

HighCommander4


People also ask

What is multithreading C++11?

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++.

What are C threads?

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.

What header file should you include for using C 11 multithreading capabilities?

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.

What is the use of C++11?

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.


1 Answers

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.

like image 122
aaronman Avatar answered Oct 22 '22 22:10

aaronman