Thread safety, some basic questions about accessing mutable objects across threads

2013-08-06 Thread Nick Rogers
Hi,

Please look at the following situations:

1. Created a mutable array in main thread, can I read its values in a secondary 
thread safely, while no other thread is modifying this mutable array?

2. I need to add objects to this mutable array from a secondary thread, and no 
other thread is modifying it. Can I simply add an object like, [array 
addObject:obj]; or if this is wrong how to achieve the same? The object to add 
is mostly NSString or NSNumber or a mutable dictionary of NSString and NSNumber.

Please help.

Best,
Nick


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Thread safety, some basic questions about accessing mutable objects across threads

2013-08-06 Thread Abdul Sowayan
Nick,

In your secondary you can use dispatch_async or dispatch_sync to schedule a 
block of code to execute on the main thread. Below is a simple example:

dispatch_async(dispatch_get_main_queue(), ^{
  // put your code to modify mutable array here
  });


I hope this helps.

Thanks,
Abdul

On Aug 6, 2013, at 11:39 AM, Nick Rogers 
roger...@mac.commailto:roger...@mac.com wrote:

Hi,

Please look at the following situations:

1. Created a mutable array in main thread, can I read its values in a secondary 
thread safely, while no other thread is modifying this mutable array?

2. I need to add objects to this mutable array from a secondary thread, and no 
other thread is modifying it. Can I simply add an object like, [array 
addObject:obj]; or if this is wrong how to achieve the same? The object to add 
is mostly NSString or NSNumber or a mutable dictionary of NSString and NSNumber.

Please help.

Best,
Nick


___

Cocoa-dev mailing list 
(Cocoa-dev@lists.apple.commailto:Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at 
cocoa-dev-admins(at)lists.apple.comhttp://lists.apple.com/

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/asowayan%40vectorworks.net

This email sent to asowa...@vectorworks.netmailto:asowa...@vectorworks.net

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Thread safety, some basic questions about accessing mutable objects across threads

2013-08-06 Thread Kyle Sluder
On Aug 6, 2013, at 8:39 AM, Nick Rogers roger...@mac.com wrote:

 Hi,
 
 Please look at the following situations:
 
 1. Created a mutable array in main thread, can I read its values in a 
 secondary thread safely, while no other thread is modifying this mutable 
 array?

Yes, but the second half of that sentence is the tricky part. One typically 
uses a lock to ensure that no other threads are modifying the array.

 
 2. I need to add objects to this mutable array from a secondary thread, and 
 no other thread is modifying it. Can I simply add an object like, [array 
 addObject:obj]; or if this is wrong how to achieve the same? The object to 
 add is mostly NSString or NSNumber or a mutable dictionary of NSString and 
 NSNumber.

Yes, NSArray can be used from any thread, as long as if one thread is modifying 
the array it is the only thread accessing the array. Again, you need to use a 
synchronization primitive to ensure this is the case.

--Kyle Sluder

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Thread safety, some basic questions about accessing mutable objects across threads

2013-08-06 Thread Jens Alfke

On Aug 6, 2013, at 8:39 AM, Nick Rogers roger...@mac.com wrote:

 1. Created a mutable array in main thread, can I read its values in a 
 secondary thread safely, while no other thread is modifying this mutable 
 array?

Yes. Most non-thread-safe objects don’t care what threads you call them on, so 
long as they’re only called on one thread at a time. So you can use locks or 
@synchronized blocks to coordinate access.

(The exceptions are higher-level things that use runloops or dispatch queues, 
such as NSURLConnection, NSStream, etc. Those usually need to be called only 
from the same thread/queue they’re scheduled on, because they’re also getting 
callbacks on that same thread/queue.)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Thread safety, some basic questions about accessing mutable objects across threads

2013-08-06 Thread David Duncan
On Aug 6, 2013, at 8:39 AM, Nick Rogers roger...@mac.com wrote:

 1. Created a mutable array in main thread, can I read its values in a 
 secondary thread safely, while no other thread is modifying this mutable 
 array?
 
 2. I need to add objects to this mutable array from a secondary thread, and 
 no other thread is modifying it. Can I simply add an object like, [array 
 addObject:obj]; or if this is wrong how to achieve the same? The object to 
 add is mostly NSString or NSNumber or a mutable dictionary of NSString and 
 NSNumber.


Honestly I would recommend a thread-confinement strategy. If you have some 
other thread that wants to add an object to the array, it should message the 
thread that owns that array (I'm guessing the main thread here) and tell it to 
add the object.

TN2109 discusses this strategy along with sample code: 
http://developer.apple.com/library/ios/#technotes/tn2109/_index.html
--
David Duncan


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com