Hacker News new | past | comments | ask | show | jobs | submit login
C Finally Gets A New Standard (drdobbs.com)
144 points by Tatyanazaxarova on April 10, 2012 | hide | past | favorite | 70 comments



Usually the latest draft is available for free and has the same content as the standard. That should be the N1570 document.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf


My biggest problem with the new standard is the awkward naming of the threading functions. "mtx" over "mutex" saves a whole 2 bytes. Now what's up with that? Trying to avoid a namespace clash with some existing widely used library or something?


I think because such naming is used in all standard C library.


That's usually something that bothers me about C and it's libraries. I don't see the point in saving those bytes when we aren't really typing them anymore (because of modern text editors and IDEs.) Abbreviation makes the code look uglier and more cryptic, I know that appeals to some coders, but not me.


If you don't like it use a typedef to make your code more "readable". Or a macro that will replace every occurrence of mutex in your code with mtx.

However if you want to share your code with other people, I would go with the standard names.


Shouldn't the standard be as readable as possible and then use a typedef if u r 2 lazy 2 type 2 mo lettrs?


Is mutex more readable than mtx? Readability only matters for the programmer that is supposed to know what mtx/mutex is (regardless of the name) and the name itself of course, naming it mtx rather than mutex incurs no extra overhead to learn or understand. If anything the code is less cluttered and more to the point (without sacrificing anything (IMO)), but I don't see why I'd care.

Designing for people that don't know anything about what they do isn't a goal worth pursuing, the only thing you might succeed with is tricking someone into thinking they know something - in which case you've just made it worse.


Yes, it definitely matters I think. It's just an extra little bit of work to understand, which when you are debugging you needs as little mental clutter as possible. Why make something more work than it needs to be? Code is read more often then it is written therefore one ought to optimise for reading.

Also, "mtx" could just as well be "matrix" as another posted mentioned they had used in their code. "mutex" can never be "matrix"


Of course readability matters. But you must know language constructs anyway, no need for them to be overly explicit and be in the way of your code - therefor it could easily be argued that "mtx" is easier and more optimized for reading than "mutex".

The only reason an "if" or "while" statement makes sense to every programmer isn't because it is readable but because you must know the syntax and semantics of the language you program. And having a good shorthand is faster to read and since mtx isn't a word it isn't associated as anything that could be a word but you instantly see it as a language construct (kind of like syntax highlighting) and thus reduces the work of mentally parsing the code (one advantage of not writing your code in English is that you subconsciously see the language constructs differently than any of the code that you wrote yourself, not saying that I prefer to do this or recommend it (on the contrary)).

Again, this example alone isn't one I'd really care about - just questioning that "mutex" would be more readable than "mtx" (as a language construct).


I think I'm going to flip this around and say can you argue for why the keyword shouldn't be to use the "default position" of using the real word when its only two characters more?


The flaw in your argument is that what's "readable" changes dramatically over time. Once a programmer has used it enough times, "mtx" will be every bit as readable as "mutex". The brain readily tokenizes what it's seen before. Thus the case for plain-English-readability in code is limited to programmers who are not yet familiar with a notation – not a strong general case. We ought to have learned this from COBOL and every other attempt to make programming languages be like natural language.

Code is read more often than it is written, therefore one ought to optimise for reading

But by the same logic, code is read more often by people who know the language and most often by people who have been working on the program for a while, so they are the ones we should optimize for. The readability test that matters for maintaining a complex system over time is hardly "can someone unfamiliar with both the language and the program dive in to any random function and make out what it's doing". So why is that the standard always held up in discussion?


When I google search for just mtx, it's all car audio, whereas mutex gives a wikipedia result for mutual exclusion as the first link.

Not everybody will recognize mtx as mutex while reading code, and lots of people learn code by reading it, rather then from standards.


Hover mtx in your IDE once and bam, now you know.

To have a distinction between mutex and "mtx c" on google will probably be of value as well.


mutex: obviously refers to mutex

mtx: maybe mutex? maybe multi-transaction? maybe library prefix? vendor prefix? maybe, maybe...

2 characters


Back when C was first pioneer'd this was an issue. However this is the 21st century and yes we could have better full names but the standard from before is kept for consistency. I think deprecation and renaming of everything is out of the question at this point as companies and developers have invested considerable amounts of time in building applications using the good ole "fprintf" or "malloc".

I wonder what the ramifications would be if someone went through and properly named the functions.


You mean back when C was first creat'd.


I recently found myself reading Mason & Dixon and trying to figure out what the rule is for those apostrophes. Pynchon wrote that book in a hybrid of 18th and 20th century English (which is brilliant and possibly also annoying) and boy does he apostrophize. At first it seemed like all the past forms were apostrophiz'd but then I noticed many of them were elongated after all. It appears that the rule is to spell out the "e" in "ed" when it's articulated and use an apostrophe when it's unpronounc'd. I suppose this ought to have been obvious from first principles but I only hit on it by painstaking induction :)


To be fair, it should be "created." I was making a joke based on the fact that the original function name to create a file in C was in fact called "creat". If I added the (pronounced) "e", then the joke would be lost!


Ah, I missed that. Creatted? :)


This is annoying. This will conflict with the matrices in my code.


mtx_init and mtx_destroy might. The rest of the mtx_* keywords don't seem like they'd be used for a matrix library.

(Keep in mind: identifiers ending in _t are reserved by POSIX. http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh...)


Haven't read the document yet, so I don't know if there's a rationale for naming.

It might simply be racial memory, from the days of linkers with eight character symbols (and the initial underscore ate one of them). It's hard to believe we actually wrote software under those limitations.



The threads.h header comes directly from the Dinkumware Compleat C library. I would have preferred a stdthread.h with more pthread.h like struct/function naming.


It seems to me if ANY one group of people should be allowed to add new keywords to the user namespace, it should be the standard bearers. The whole _Keyword thing is something only a committee could love.


The whole _Keyword thing is to make it easier to port from older C standards to the new standard. It's not really supposed to be programmer-visible, only a workaround for compilers. There is an extensive explanation about the reasoning behind this solution in the spec. Read that and come back if you still have a problem with making forward-compatibility hacks.


On a similar note, what is up with thrd_timedout, thrd_success, thrd_busy, thrd_error, thrd_nomem?

Why no ea's?


That's horrible. I read it the first time as "third success." At least "mtx" is a little easier to see (though still quite silly).

Can anyone defend this?


The Cxx committees try really had to make everything backwards compatible. This includes trying to not step on existing namespaces. These names, along with mtx, etc., are just going overboard though.


If the C committees want to avoid namespace collisions, why not use _Keyword style names like _Mutex_init and _Mutex_destroy?


I see they made the same mistake as POSIX threads in implementing recursive mutexes. Disheartening.


Of all the ways to shoot yourself in the foot with C, recursive mutexes seem relatively benign. The implementation is a handful of lines of code, and they're not always a symptom of bad design, either; imagine needing to lock each node in a path through a (possibly cyclic) graph.


If two different threads were trying to lock different paths that entered the same cycle at different nodes, there can be a deadlock.

  X -> B
  B -> C
  C -> B
  Y -> C
Thread 1 tries to lock X B C; thread 2 tries to lock Y C B; deadlock with 1 holding X and B, 2 holding Y and C.

It's possible that your paths may be more restricted than this, but I reckon keeping the paths and cycles clean would be a bigger problem than recursive mutexes.


Recursive mutexes are useful when you need to implement multithreading a posteriori and cannot evaluate all the locking paths.


When adding locking without evaluating all locking paths, how can you be sure none of those paths cause a deadlock ?


You cannot self deadlock with a a single recursive mutex.


Would anyone here be interested in a shim to play with this stuff before the compiler vendors add support?

We've got a library that mirrors a lot of this functionality really closely already.


I would bet that most people who want to use it also have a similar library. For the thread stuff I just adhered to the pthread interface, and for atomic operations, I grabbed similar functions from the Linux kernel. (If you need code to do a low-level systems thing, chances are the Linux kernel needs it too. Fantastic resource.)

I see this as a standard that's less providing new things, but providing consistent names and interfaces to things many people already do. Personally, I had always seen Pthreads as the defacto thread, mutex and condition variable standard for C. But it makes sense to define one outside of Pthreads for non-POSIX platforms, particularly if you already need to add atomic and thread-local to the standard.


Our library is mainly focused on cross-platform (Windows, Linux, OSX, Solaris, etc.) and developer ergonomics (Good documentation, orthognal and clean features, descriptive names, etc.).

But yes, I do agree with your first paragrpah. And we're always open to friendly emails--see profile for contact info.


Good documentation? I'm interested.


The stuff on our site (see profile) is pretty well documented in the .h files... our next release (hopefully in a week or two) will offer something like MSDN-style overview pages along with detailed function docs.

Really wish there was something like rdoc for C code--and no, Doxygen is fugly and fail.


Anyone have visibility on what the support for this looks like?


Clang and GCC both have limited support for C11 currently, which can be enabled by compiling with -std=c11.


I would expect some support from gcc and clang.



I haven't seen anything about C11 there, only C++11.


1990 C90

2000 C99

2011 C11

so, if this is the trend expect a new C standard in 2022.


True, but new standards don't mean that they get support in all compilers.

http://en.wikipedia.org/wiki/C99 http://en.wikipedia.org/wiki/C11_%28C_standard_revision%29

Microsoft as typical bad boy attitude already said it only cares about C89, as everything else can be done with C++ anyway.

And as they don't care about portability this is unlikely to change, unless they are pressured to change like it happened with IE.


Nobody is obliged to implement a new standard. C90 is a Standard, C99 is another one. 'Newer is better' is a fallacy. SQL 92 Intermediate Level e.g. is the most important SQL Standard.


True.

Do you know if SQL 92 is finally fully supported across DB servers?

This was a nightmare back in the early 90's.


At least SQL 92 is the 'common denominator' among current relevant RDBMS.


To be honest, C99 is not a true subset of C++.


No, 2023


C/C++ is used to build the fundation of pretty much everything you use every day. I don't think a fast moving standard would be a good thing.


A fast moving standard is not necessarily a bad thing, provided that changes don't break backwards compatibility.

In some ways, I'd like the C standard to move a bit faster, to keep up with things like multi-threading (although there's an argument to be had over whether that should be defined as part of the language or left to vendor-specific libraries).


> A fast moving standard is not necessarily a bad thing, provided that changes don't break backwards compatibility.

Another problem is that the standard becomes irrelevant. This is a concern for HTML5, because a 'rapidly evolving' standard means that things can be added prematurely - but once something is in the standard, it can't be removed (easily). At some point, you run the risk of having more companies go the Microsoft route and just abandon the standard altogether, and either stick with an older version, or just fork it into their own new language.


Aaaaand that's why they've added these ops. Note that it took a while (> decade) to really find out that these features were reasonable to assume support for. :)


> C/C++

This is specifically about the C programming language. Can we not conflate it with a different language entirely, at least in this comment thread?


I was under the impression the C++ standard moves in lockstep with C so that all valid C programs should also be valid C++ programs.


I believe that has not been true for a long time, at the very least because some things are keywords in C++ that would be valid identifiers in C.


Yea, after looking into it they don't seem to be completely compatible anymore. I have known many people to use a C++ compiler when writing C code, but it looks like that restricts you to a subset of C.

They do try and stay reasonably consistent so for example Long Long moved from C to C++. And C99 has reduced some other incompatibilities by incorporating C++ features such as // comments and mixed declarations and code.

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B


"I have known many people to use a C++ compiler when writing C code, but it looks like that restricts you to a subset of C."

Many C++ compilers actually are C/C++ compilers that decide what language to compile depending on command line flags and/or file name extension.


Ahh, ok now that explains a lot, thanks.


That's Objective-C.

    int* foo = malloc(sizeof(int));
Is invalid C++.


I don't really know C++ as well as I know C - could you explain? I always thought that the only real reason C++ wasn't a superset is because of the additional keywords.


C++ doesn't coerce the void* malloc returns into an int* , C does. You have to write

    int* foo = (int*)malloc(sizeof(int));
in C++ or the compiler gives a type error.


/me looks at watch

Why did they name this standard C11 and not C12? To preserve name parity with C++11?


It was standardized in late 2011. DrDobbs just takes it's DrTime to get out those DrArticles. :)


C++'s const correctness rules?


I don't know what exactly you are asking, but no, C11 doesn't have that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: