Re: How to tokenize string without using strtok
- From: Chris Torek <nospam@xxxxxxxxx>
- Date: 17 Feb 2006 04:49:21 GMT
On Thu, 16 Feb 2006 19:26:05 +0000, Walter Roberson wrote:
Nothing in C89 gives test-and-set assurances ...
(This is true for C99 as well. On every real machine that really
supports multiprocessing and/or threads, one can write a suitable
mutual exclusion routine, callable from C code, but it is rare that
one can write a good one even in machine-specific C. It usually
requires some kind of assembly code: either inline __asm__, or just
a simple C-callable assembly routine.)
... so although you could
possibly read the sig_atomic_t variable, test its value, and write a new
value based upon what you found there, you don't know that you won't have
had an interrupt between the time of the read and the time of the write,
so you might clobber a mutex lock that something else has written there
while you blinked.
In article <pan.2006.02.17.00.24.18.951591@xxxxxxxxx>
Ben Bacarisse <ben.usenet@xxxxxxxxx> wrote:
I think I see what you are talking about. I thought you were making a
more general point -- that one could not, even in principle, implement a
mutual exclusion algorithm (such as Peterson's) with what little
assurances standard C provides.
You cannot, because on some real multiprocessor machines, memory
synchronization instructions are required to obtain store/load
sequencing guarantees. Consider the following "obvious" code:
# cpu 4 # cpu 7
store reg,[addr] nop
nop store reg,[addr]
load reg,[addr] nop
where instructions written on the same source line are executed in
the same clock tick, and [addr] is the same on both machines. You
would expect that the "load" on CPU 4 would see the value stored
by the "store" on CPU 7. But in fact, it may not (this depends on
unpredictable events like interrupts, making debugging difficult).
To make the result predictable, and make cpu 4 "see" cpu 7's store,
we have to replace the "nop" no-ops with "memory synchronize"
instructions (possibly in several flavors, and maybe only one is
required on each CPU; it depends quite a bit on the CPU architecture).
While the Java language has built-in thread and locking support,
it turns out that the locking was broken on some architectures,
such as the Alpha. Getting it right was tricky; a committee spent
years defining the memory model. See
<http://www.cs.umd.edu/~pugh/java/memoryModel>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.
- Follow-Ups:
- Re: How to tokenize string without using strtok
- From: Ben Bacarisse
- Re: How to tokenize string without using strtok
- References:
- How to tokenize string without using strtok
- From: bubunia2000
- Re: How to tokenize string without using strtok
- From: Ben Bacarisse
- Re: How to tokenize string without using strtok
- From: Walter Roberson
- Re: How to tokenize string without using strtok
- From: Ben Bacarisse
- How to tokenize string without using strtok
- Prev by Date: Re: Flexible arrays - v confused
- Next by Date: Re: scanf (yes/no) - doesn't work + deprecation errors scanf, fopenetc.
- Previous by thread: Re: How to tokenize string without using strtok
- Next by thread: Re: How to tokenize string without using strtok
- Index(es):
Relevant Pages
|
|