Re: Threads and global variables (was:Programming style revisited)
Re: Threads and global variables (was:Programming style revisited)
- Subject: Re: Threads and global variables (was:Programming style revisited)
- From: Robert Goldsmith <email@hidden>
- Date: Sat, 17 Aug 2002 12:41:49 +0100
>
> I.E.. The following scenario will result in a deadlock on most MP
>
> architectures:
>
>
>
> Thread 1 does:
>
> myVar= 10
>
>
>
> while Thread 2 is doing:
>
>
>
> while(myVar<10);
>
>
>
> But even this issue isn't really the processor's fault. The
>
> compiler is
>
> sticking myVar into a register (on thread 2) and never checking memory
>
> again. This situation could certainly occur with pointers but
>
> it is not
>
> strictly a pointer problem.
>
>
and how can we solve this situation in ObjC ?
>
(I think I had the case, but then I didn't understand the problem)
I have no idea if this will work (not having an mp machine to
test it on ...) but when you are writing c code for firmware and
you have memory-mapped devices you can have a similar problem. C
will optimise, for instance:
PORTA=10;
PORTA=20;
so the first line doesn't work. This is no good when you are
actually talking to some hardware instead of memory.
The very old solution to this is the c keyword 'volatile'. Try
the following:
volatile int myVar;
This -should- force c to re-fetch the value every time. Be warned
though, excessive use totally screws the c optimisation process
because volatile variables are kept at arms reach by the comiler
(it does very little with them).
Robert Goldsmith
---
GnuPG public key:
http://www.Far-Blue.co.uk/RSGoldsmith.asc
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig; charset=US-ASCII]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.