• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Thread question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Thread question


  • Subject: Re: Thread question
  • From: Hisaoki Nishida <email@hidden>
  • Date: Wed, 13 Nov 2002 01:18:45 -0500

Hi Nathan, thank you for the help.

I was mistaken with protected memory. I thought they would eliminate things like having to worry about simultaneous write/read from 2 threads. It only meant protection between application memory bounds.
The global variable is a struct of type fd_set (struct used in BSD sockets). I think it is far larger than 32 bits, it could be 1024 bits, I'm not sure. I am not familiar with the assembly level detail, but I think I should be using an NSLock. I don't really care when my main thread writes to the global variable, as long as the 2 threads don't do a write and a read at the same time. In fact, I expect my main thread to change the variable when it wants to. No other restrictions.
My separate thread is actually a method that handles server logins.
I feel the need to use a separate thread for this because my main thread deals with sends and receives from connected clients. So it can't afford to wait for login sessions to be complete (which could take long/timeout due to disconnects/lag/other errors).
I just wanted to know that when you say I need a lock when I only care when the variable is changed, do you mean that there is a need to regulate when the variable changes so that the read can be performed after the write. Then I definitely need a lock, it seems.

Thanks again,

-Yuki


On Monday, November 11, 2002, at 06:50 AM, Nathan Day wrote:

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/



// Hisaoki "Yuki" Nishida
// <email@hidden>
_______________________________________________
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.

  • Follow-Ups:
    • Re: Thread question
      • From: Nathan Day <email@hidden>
References: 
 >Re: Thread question (From: Nathan Day <email@hidden>)

  • Prev by Date: Re: Hit detection on shapes within window
  • Next by Date: [Semi Off] General Interface Speed
  • Previous by thread: Re: Thread question
  • Next by thread: Re: Thread question
  • Index(es):
    • Date
    • Thread