Subj : Re: Adding thread support for C... To : comp.programming.threads From : Mayan Moudgill Date : Fri Jul 22 2005 04:10 pm Joe Seigh wrote: > Mayan Moudgill wrote: > >> >> I was wondering what features people wish "C" had that would make life >> easier. [Obviously, we're talking about extensions to C, not standard C]. >> >> For instance, we could add a statement __barrier() {don't reorder >> memory operations past this statement}, or a non-stack scope variable >> qualifier __thread {this variable is thread private}. >> >> Anything else people wish they had in the language? >> > > Actually, we're probably better off without it. Once they put thread > support > in C we'll be in the same situation Java is in where you can't add anything > without adding support directly in Java itself, e.g. JSR 166. Where do > you > think the stuff they added in JSR 166 came from? > Interesting. Our original motivation for extensions came from the desire to write various atomic operations in C. We wanted to do: void atomic_increment( volatile int * p ) { do { int x = __load_locked(p); x++; } while( !__store_conditional(x,p); } [We've already added two extensions, which can be modelled as functions]. We found that the compiler was: a) inlining functions of this form. b) then moving code around in somewat unpleasent ways. We introducted the __barrier(); "call" to inhibit the compiler from doing this. [__load_locked/__store_conditional are also __barriers before/after the operation]. Another problem came from spin-lock wait loops. void spin(unsigned n) { unsigned i; for( i = 0; i < n; i++ ) { } } The compiler was eliminating the wait-loops [as it should]. We had to introduce a mechanism to inhibit this optimization. It turns out that, as the compiler starts optimizing more, and worse, doing whole program optimizations, it becomes critical with multi-threaded programs to make sure that the compiler can distinguish between optimizations that would be correct in single-threaded C but are invalid in multi-threaded codes. .