73

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • See also: http://stackoverflow.com/questions/4938258/where-can-i-find-good-solid-documentation-for-the-c0x-synchronization-primiti – bdonlan Jan 16 '12 at 06:17
  • 21
    @bdonlan: The question is about c11 and not c++11.Both are different.Both the links you provided are for c++11 and not c11.duh? – Alok Save Jan 16 '12 at 06:19
  • 3
    I'm close voting ('exact duplicate') because your first question (About the sequencing model) has already been answered. It's difficult to answer questions that contain multiple questions, because someone may only know about part of your question - their answer can therefore never be accepted, because it doesn't answer the whole question, and there may never be an accepted answer. Please split your question up, and remove the exact duplicate part (or indicate why it's different from the questions I linked). – bdonlan Jan 16 '12 at 06:25
  • 2
    ... oh, I now see it's about C11, not C++11. Nevermind then! I still recommend that you split your question up however - you have a question on 1) What is a memory sequencing model? (possibly answered by the C++11 one, the answer will be similar with different syntax) 2) IS this all that C11 provides? 3) Is pthreads etc still needed? 4) How is C11 compiler support progressing? – bdonlan Jan 16 '12 at 06:25
  • 1
    @bdonlan: I believe `1-3` Q's are logically related and should be grouped together.An answer for any of the 3 would touch upon all 3 of them.Perhaps, `4` could be a separate Q but I didn't feel justified starting an Q just for that. – Alok Save Jan 16 '12 at 06:31
  • 1 is a very technical question that would need a long answer like the one from the C++11 question I linked; 3 is one that someone with less deep knowledge of the APIs involved could answer. 2 requires even less specific knowledge. It's up to you if you want to split it or not, but complicated multi-part questions tend to get fewer answers, and targeted questions will be more helpful for people in the future searching for something specific. – bdonlan Jan 16 '12 at 06:33
  • @bdonlan: I understand your concern.However,I will let it stay as-is for the moment, Until at-least some answer shows up,if theres none which touched upon them I will sure split them up. – Alok Save Jan 16 '12 at 06:36
  • Voting to close as too broad: multiple questions in one. Implementation status asked at: http://stackoverflow.com/questions/8741299/does-an-available-compiler-provide-an-implementation-of-the-c11-atomic-keywor and http://stackoverflow.com/questions/24557728/does-any-c-library-implement-c11-threads-for-gnu-linux – Ciro Santilli OurBigBook.com Jun 16 '15 at 22:09

3 Answers3

58

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.

Community
  • 1
  • 1
janneb
  • 36,249
  • 2
  • 81
  • 97
  • Thanks! A few comments,I am not entirely convinced by the argument concurrency in c11 is inspired or imported from C++11.Any credible references which say so will build up my faith towards accepting it.About need of POSIX & Multithreading libraries in future,a user will not have to bother ensuring having those in place because the standard now mandates that an implementation has to have them in place.So even if the standard lib api are just wrapper over native libs it is responsibility of the implementation to provide the entire package unlike before.Nice links on the compiler support though. – Alok Save Jan 17 '12 at 05:22
  • @Als: I added some links about the C++ heritage of the concurrency work. Wrt your second point, keep in mind that 1) it will be a long time until C11 is common enough that one can assume it's available 2) even so, threads and atomics are an optional part of the standard, see e.g. the __STDC_NO_THREADS__ and __STDC_NO_ATOMICS__ macros. That being said, IMHO the memory model is a big step forwards as it specifies how the compiler should behave wrt memory accesses by multiple threads, which helps programs using pthreads just as well as those using the C11 threads. – janneb Jan 17 '12 at 08:28
  • @Als: Added one more link (n1349). Also, sorry for the formatting fail in the previous comment. – janneb Jan 17 '12 at 09:39
  • Is there any standard way to request a "compiler memory access" barrier that would require a compiler to ensure all pending writes get issued to the underlying platform before execution passes the barrier, and all reads examine a value that existed sometime after the barrier? Many platforms have multiple compilers available, and it would be helpful for a program written for a particular platform to be able to run reliably on all compilers for that platform, and the notion of creating a compiler-memory-sequencing barrier hardly seems obscure. – supercat May 12 '16 at 22:30
  • As of Visual Studio 2017, there is a header called `xthreads.h` (you'll find under the `thr` folder in the include directory). It's nearly identical to `threads.h`. But I believe this was only created to supported the C++ threads implementation. Still, it works just fine. – annoying_squid Feb 21 '19 at 14:08
  • According to [cppreference](https://en.cppreference.com/w/c/language/atomic), compatibility is only recommended, not required. So, depending on your particular compiler (such as MSVC), your mileage may vary. So, continuing the standards trend of "we're only standardizing portable syntax, not portable behavior". >Implementations are recommended to ensure that the representation of _Atomic(T) in C is same as that of std::atomic in C++ for every possible type T. The mechanisms used to ensure atomicity and memory ordering should be compatible. – BTJ Jan 10 '23 at 20:27
8

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.

Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
3

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 1
    "threads.h" is imho going to be part of C libraries (such as glibc) not just compiler. No current C library and compiler has full support for "true" multithreading (for now and near future as of 02/2k12). – Tomas Pruzina Feb 04 '12 at 15:31
  • @AoeAoe, P99 also contains a complete emulation of the "threads.h" part on top of POSIX threads, including `mtx_t`, `cond_t` and stuff like that. – Jens Gustedt Feb 04 '12 at 16:19
  • 2
    What defines "gcc family"? There's no relationship that I know of between the GNU C compiler, the Intel C compiler, and Clang. – bames53 Oct 29 '12 at 15:55
  • 7
    @bames53, there is the simple relationship that these compilers seem to try to be in large parts compatible to gcc. In particular they all define the `__GNUC__` macro to some value. – Jens Gustedt Oct 29 '12 at 16:01