Multi-Threading support in c11

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:

  • Thread creation and Management
  • Mutex
  • Conditional Variables
  • Thread Specific storage &
  • Atomic Objects

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?


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:

  • n1349
  • n1423
  • n1424
  • n1437
  • n1479
  • n1480
  • n1489
  • n1584

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?


Pelles C supports C11 <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.