Showing posts with label Standards. Show all posts
Showing posts with label Standards. Show all posts

Friday, November 09, 2007

Re: "C and multithreading"

On debian planet. I read the C and multithreading post of Miriam Ruiz, but I don't agree with some conclusions:

First of all, I don't agree with Linus. C standard doesn't allow such optimizations. One of the driving point of C standard (and thus C) is that compiler should do what a programmer write, without much optimization, i.e. C should remain a low level language. It was iterated also for the principles of new C1X standard.

Second point. Volatile is not the right solution. volatile means that a variable should be read every time it is accessed, so the variable should not be but in registers (remember the nearly obsolete register keyword).

The problem of multi threading is not only that variables could changes (but this is a fact also of
single thread programs, when you write a signal handler [which are specified in C standard and which are handled correctly]), but for semaphores there are "barriers", i.e. you should not move read or write across such barriers.

What do volatile have with barriers? Nothing. If you read the standard (you can check also only the one page C appendix), you see that C specify what are the "standard" barrier, and they are nearly in all obvious points (i.e. a ";" is also a barrier), so moving instructions is "illegal" for C.
Ok. volatile had few other barriers, but across ";" there is already a barrier, so volatile is not the solution of barrier problem (AB locking).

References: see C wiki and C standard.

Tuesday, October 17, 2006

C99

Yesterday I was looking for the changes between the ANSI/ISO C (1989) and the new C99 (ISO 1999). The best information are found in: http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.htm
Summary of the main changes:
  • external identifier now are case sensitive and up 31 characters (in old standard a portable program should not rely on case sensitiveness nor to more of 6 chars)
  • variable argument on macros
  • from C++: // comment, inline, declaration and statement can mix, for allow declaration in first "argument"
  • compound literals
  • initializers: int a[10] = { [5] = 3}; struct {int a,b} c = { .b=3};
  • long long (64-bit or longer) with LL, ll, ULL and ull suffixes in costants
  • __func__ "automatic variable" : void f(int void) { fprint("me is %s\n", __func__); }
  • _Bool and if you #include you have bool, true and false
  • restrict keyword to optimize function: you tell compiler that pointers don't point overlapping regions.
  • variable-length arrays
The gcc has a page about what is implemented and what not.

In http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899 you see the complete standard with the two corrigenda, and the rationale about the new standard. It worth to browse it!