Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppKit and Thread Safety

AppKit and Thread Safety
Thread Tools
Dalgo
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jun 21, 2001, 07:48 PM
 
I am not sure if this is a safe thing to do or not. (Pesky lack of documentation) I want to have 2 threads that work on 1 NSMutableArray. I am going to use the array like a queue. One thread reads and removes stuff from the front, the second thread adds to the end. I have already thought out how I am going to synchronize the addition and stuff so that if the thread removing stuff from the queue finds [queue count] == 0, then it will just wait for new data via a condition lock.

Do I need to use a lock when I remove something or add something from the NSMutableArray? I can think of an instance where, one thread is trying to remove the last item while another is trying to add something to the end of that item and everything could get messed up if it wasn't thread safe. So: Is NSMutableArray thread safe or will I need to use my own locks to control addition/ subtraction of stuff?
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jun 21, 2001, 09:22 PM
 
In general, mutation of mutable collections is not thread-safe. One reason for this is performance; second reason is that you often need some external lock anyway to coordinate some of the top level stuff you're doing. For instance, NSMutableArray could not help you with enumerating over the items if it is being changed by another thread.

The ThreadSupport.html release note has more details about thread safety in AppKit & Foundation...

Ali
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:25 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,