The new C11 standard provides a support for Multi-Threading.
My Questions are a bit diversified but definitely answerable.
I have had a look at the C11 n1570 draft.
It says:
support for multiple threads of execution including an improved memory sequencing model, atomic objects, and thread-local storage (
<stdatomic.h>
and<threads.h>
)
What is the Improved memory sequencing model? How/What changes from the c99 Standard?
Rather than just quotes from standard, I will appreciate if someone delves deeper in them and tries explaining the semantics involved.
As I understand, C11 provides support for:
I hope I didn't miss anything?
Since now the Standard library itself provides(will provide) all the functionalities needed for Multi-Threading, there would be no need for POSIX and such libraries(for Multi-Threading support) in future?
Lastly, What compilers provide support for the above mentioned features? Are there any references as to timelines when these will be supported?
I remember for C++11 there was a link for compiler support and features, perhaps something like that?
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++.
C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.
Multithreading is the ability of a program or an operating system to enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user.
Starting with C++11 C++ has classes for multithreading support. The class you might be interested in most is std::thread . There are also classes for synchronization like std::mutex .
First, don't write off C++11. The concurrency work for the new standards was done under the C++11 umbrella, then imported into C11 with the explicit goal of being compatible. While there are some syntactical differences (e.g. due to plain C not having templates or function overloading), semantically they are identical by design. For "evidence" of this, one can check the WG14 papers. E.g:
and references therein. More can be found at Open Std Website
Now, on to your questions:
What is the Improved memory sequencing model?
The obvious answer is that it has been changed to take into account multiple threads and how they interact. For a slightly longer answer, see C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming? that was already mentioned in the comments. For an in-depth understanding, a stackoverflow answer is perhaps not the right place (even less a question with several subquestions!). But luckily Hans Boehm maintains a very good page with interesting links for further reading (again, keep in mind that the C11 and C++11 memory models are semantically identical)
I hope I didn't miss anything?
Together with the memory model, your list seems to cover the concurrency additions in C11. For other changes, wikipedia has a list; of the top of my head I can't come up with anything the wikipedia list has missed.
Since now the Standard library itself provides(will provide) all the functionalities needed for Multi-Threading, there would be no need for POSIX and such libraries(for Multi-Threading support) in future?
Yes, there will be a need for them. First, nobody is going to rewrite all the existing code that uses the various existing thread API's. Secondly, the C(++)11 thread library is/will most likely be implemented as a wrapper around the various native thread libraries; heck, there's even a documented way to retrieve a pointer to the underlying native thread, in case one needs to do something beyond what the C(++) thread library supports. Think of the C(++)11 thread library more like a portable, least common denominator wrapper around the various native thread libraries.
Lastly, What compilers provide support for the above mentioned features? Are there any references as to timelines when these will be supported? I remember for C++11 there was a link for compiler support and features, perhaps something like that?
I haven't seen any detailed list, there doesn't seem to be as much buzz around C11 compared to C++11. There's a short notice for the upcoming GCC 4.7 here: http://gcc.gnu.org/gcc-4.7/changes.html . For the concurrency support, one can check the support for concurrency in the C++11 status page here: http://gcc.gnu.org/projects/cxx0x.html . There's also some notes on the current status and plans for GCC at http://gcc.gnu.org/wiki/Atomic (according to that page, stdatomic.h is available). For other compilers, there's a nice list of the C++11 status for various compilers here http://www.aristeia.com/C++11/C++11FeatureAvailability.htm . From the links there one can check the status of the concurrency support, and assuming that the vendor in question plans to support C11, the C11 concurrency support is then likely to be at about the same level.
Regarding What compilers provide support for the above mentioned features?
<threads.h>
. Creation of threads with Pelles C
compiler example: #include <stdio.h> #include <threads.h> #define NUM_THREADS 7 static int threadData[NUM_THREADS]; int threadFunction(void * data) { printf("%d-th thread up\n", *(int*)data); return 0; } int main(void) { thrd_t threadId[NUM_THREADS]; // init thread data for (int i=0; i < NUM_THREADS; ++i) threadData[i] = i; // start NUM_THREADS amount of threads for (int i=0; i < NUM_THREADS; ++i) { if (thrd_create(threadId+i, threadFunction, threadData+i) != thrd_success) { printf("%d-th thread create error\n", i); return 0; } } // wait until all threads terminates for (int i=0; i < NUM_THREADS; ++i) thrd_join(threadId[i], NULL); return 0; }
EDIT: Eliminated thread shared data problem and problem of exiting from main()
earlier than all threads terminates.
Janneb already has given a lot of explanations. For your last questions
Lastly, What compilers provide support for the above mentioned features? Are there any references as to timelines when these will be supported?
The gcc family of compilers (clang, icc, opencc) supports most of the semantics that the new standard requires, there are only syntactical differences. (clang even implements _Generic
in the latest version.)
For P99 I have written wrapper macros that map most of the features to something that is already C11 syntax, or comes close to it (for emulating _Generic
).
So if you have one of these compilers and are on a POSIX system, you can start to use a lot (most) of C11 immediately: threads with all the types mtx_h
etc, atomics with _Atomic
, type generic macros (syntax is slightly different from C11), _Static_assert
and the alignment stuff.
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