How threads are implemented in STL C ++: Are they wrapped around standard system APIs: WinAPI, POSIX, etc., or are they organized at a lower level? Thank.
- oneAuthor, are you about streams or threads? - avp
- I thought about threads of course. - alexgdi
3 answers
The standard does not indicate how to do this, but, as they say, is ready to argue that ultimately work is done through the operating system, but not at a lower level. It just doesn't make any sense!
- Judging by the mention of disk sectors, the answer about streams , however, although in the question of m. meant threads , obviously it approaches. - avp
- one@avp "By the way, young man! this is the answer to your question too ..." (c) Anecdote - I throw out a clarification from the answer, and it is suitable for execution threads ... (Knocked down, probably, the fact that right now I sit immersed in file streams ...) - Harry
- It turned out cool, the author said that he asked about threads. IMHO, he is now simply obliged to accept the answer. - avp
- why the question was asked at all, to the choice between std :: threads or conditional compilation between API functions. - alexgdi
- one> conditional compilation between API functions. - a very delicate moment. It is better to use the standard. But we have embedded (I work with ThreadX, but it also applies to any FreeRTOS) API for creating a thread that is much richer than the provided interface in
std::thread, at a minimum: we should be able to pass a pointer to the stack and its size, set thread priorities, name ( Yes, and this is, especially useful when debugging) and so on. Moreover, individual interfaces of the same pthread also allow this, butstd::threaddoes not. In such cases, you have to contact the native API. - Monah Tuk
Implementation issues.
std :: thread in libstdc ++ is implemented on top of the gthread abstraction, which, in turn, can be wrapped around what is needed. On Linux, around pthread . In general, see the gthr.h, gthr-default.h, gthr-posix.h or gthr-signle.h files in the source code.
On Windows, libstdc ++ does not provide support for threads, or rather does not provide an implementation of the gthread level for this platform. Therefore, the MinGW project provides a choice of what will be used as threading functionality: Win32 threads or winpthreads threads. When using the first, you will not have std :: thread, because there is no way to make a drop-in-place replacement. The latter, in fact, implement the pthread interface on Windows, i.e. They are an additional wrapper around Win32 threads, a kind of adapter.
I cannot tell you how threads are supported in other implementations of STL (for example, the same MSVS).
- It's funny, the first and second add-ons around Win32 threads, but the first ones don't have the ability to std :: thread. - cpp_user
Wrapper. Read the source. Everything will fall into place.