Re: Thread question
Re: Thread question
- Subject: Re: Thread question
- From: Nathan Day <email@hidden>
- Date: Mon, 11 Nov 2002 22:20:55 +1030
It depends on how you are using that variable and if it can be set in
one assembly instruction. The variable can be set in one assembly
instruction if it is word aligned and it fits into a single register,
i.e. it 32 bit or less, if you are worried about portability then this
is harder to known. This is important because if the value is set in
two instructions then the value of the variable between the two
instructions is garbage. If it doesn't matter to the secondary thread
when the variable is changed by the main thread then you don't need to
lock, you only need a lock if the secondary thread needs the variable
to only change at certain times, like only after every second time you
access it. You might want to declare the variable volatile if want
changes to the variable to be available to the secondary thread
straight away, volatile tells the compiler that the value can be change
or read externally, i.e. another thread, and so cannot be kept within a
register.
On Monday, November 11, 2002, at 08:28 AM, Hisaoki Nishida wrote:
I have an object (obj A) that has a method that runs in a separate
thread. The method uses the object's global variable. I have another
object (obj B) that posts s notification, where in the notification
center obj A is registered. As obj A receives the notification, in
it's main thread (not the separate thread) there is a method that
handles the notification. The method modifies it's global variable
that is shared with the separate thread.
Is there a need for me to prevent the simultaneous occurrence of write
and read for this variable?
Or, does Mac OS X handle this internally, using protective memory, so
there is no worry for me about this?
I know in Java you have to use 'synchronized' for this.
Nathan Day
http://homepage.mac.com/nathan_day/
_______________________________________________
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.