iOS 开发实用技术导航
NSHipster 中文版
› http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
› http://www.cocos2d-iphone.org/
CocoaPods
› http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
› http://code.google.com/mobile/analytics/
WWDC
› https://developer.apple.com/wwdc/
Design Guides and Resources
› https://developer.apple.com/design/
Transcripts of WWDC sessions
› http://asciiwwdc.com
Cocoa with Love
› http://cocoawithlove.com/
Cocoa Dev Central
› http://cocoadevcentral.com/
NSHipster
› http://nshipster.com/
Style Guides
› Google Objective-C Style Guide
› NYTimes Objective-C Style Guide
Useful Tools and Services
› Charles Web Debugging Proxy
› Smore
holonunu
V2EX  ›  iDev

NSMutableArray、NSMutableDictionary,的 copy、allValues 等方法需要加锁保证线程安全吗?

  •  
  •   holonunu · Feb 8, 2018 · 7906 views
    This topic created in 3159 days ago, the information mentioned may be changed or developed.

    addObject、objectForKey 都需要。 copy、allValues 等方法需要吗?

    3 replies  •  2018-02-20 23:17:51 +08:00
    ai277014717
        1
    ai277014717  
       Feb 8, 2018
    如果 copy 或者 allValues 时 remove 了一个或者 removeAll,或者加了一个或多个。理论上有可能发生的吧。
    如果 copy 或者 allValues 时不需要保证数据完整性,不需要加,需要的话就加呗。
    miketeam
        2
    miketeam  
       Feb 8, 2018 via iPhone
    是这样的,如果你开启多线程,然后有读有改的情况下,go 里面都是加读写锁,iOS 的话可以使用 yykit 里面的 threadsafearray,哪里面就是用了锁。如果你不想用锁,参考 cocoalumberjack 这个框架,使用队列同步的方式。你对数组和字典的读写和人家对文件的读写不是一会是么?你可以看看,最后,你倒是说说你的使用场景啊!加锁的性能要次点
    Alchemistxxd
        3
    Alchemistxxd  
       Feb 20, 2018
    多线程的情况下推荐在同一个子系统里,将所有子系统内的 queue,set target 到同一个 serial q 上,可以避免加锁,也可以避免 Context 切换带来额外的开销。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   942 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:47 · PVG 07:47 · LAX 16:47 · JFK 19:47
    ♥ Do have faith in what you're doing.