English 中文(简体)
在 NSO 操作队列中不需要锁定吗?
原标题:Locks are unnecessary in NSOperationQueue?

我读了苹果公司的文件《同时编程指南》,我认为“行动队列”是一系列行动。 该文件说,我们在“国家业务队队队”中不使用锁。

<强问/强>

  1. 如何执行 Read & amp; Write 和 NNS OperationQuue 执行任务?

  2. 何时在NSA行动队列中使用锁?

最佳回答

操作队列不一定是序列队列。 您可以在队列上调用 [queue setMaxConcurporationOpertyCount:1] , 将操作队列编成序列队列。 否则, 默认的话, NS OpertyQue 是同时的 。

您一般应该设计您提交到队列的操作, 这样它们不需要锁。 这是您的责任。 想法是操作应该自成一体。 它们应该配置在提交运行前操作所需的数据。 数据应该是一个私人副本; 它不应该是操作同时访问的操作, 而其它东西也正在访问它。 操作在运行中, 它应该只使用其私人数据工作, 不应该访问程序的其他部分。 当操作完成后, 它应该将其结果提供给程序的其余部分 。 ( 这可能是被动的, 只需将结果存储在它自己的属性中即可 。 然后, 程序的其他部分, 可能已经观察到操作完成的功能可以读取它。 或者它可能是活动的, 操作最后一件事是调用其他对象, 并发送结果 。)

NS OpertyQueue 不为读写锁定提供支持。 您可以为此使用 pthread_rwlock API。 但另一种方法是使用 GCD 发送有屏障任务的队列。 使用 dispatch_ queue_ create () 创建一个私有的同步队列。 以 dispatch_ [a]sync () 向它提交读任务。 请使用 dispatch_barrier_[a]sync () 向它提交写入任务 。

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签