question on semantics

Mike Stump mrs@apple.com
Wed May 4 22:29:00 GMT 2005


On May 4, 2005, at 12:47 PM, Chris Friesen wrote:
> One problem with using volatile is that it can destroy performance.

Gosh, I was going to elaborate and give the more complete answer, but  
decided against it, I was wrong.

> only need to really read it in from memory the first time after I  
> take a lock, and flush it out to memory at the time I give up the  
> lock.

I was going to say, you can insert memory barriers inside the, near  
the lock/unlock (or even, inside the lock/unlock code.  If your lock/ 
unlock author has put them in there for you, then, trivially, you  
don't even need them.  Since this list doesn't cover pthreads, we are  
the wrong people to answer that question.

Example:

int i;

int main() {
   int j;
   while (i)
     {
       asm volatile ("" : : : "memory");
     }
}

Here, i is treated as volatile, though, not marked as such, because  
of the memory barrier.  You can put the barriers anywhere you need them.

Also, bear in mind this list isn't about how to program using  
pthreads...

My uneducated guess is that any pthreads implementations should have  
memory barriers, and so, you could always use them and no amount of  
getting better of the optimizer would ever break this.  I'd guess  
that this would be a bug in the phreads code, if it failed to have  
such a barrier.



More information about the Gcc mailing list