ios - 有没有办法让 synchronized 关键字不阻塞主线程

标签 ios multithreading ios5 thread-safety

假设您想在 iOS 应用程序的后台做很多事情,但您对其进行了适当的编码,以便创建线程(例如使用 GCD)来执行此后台事件。

现在,如果您需要在某个时刻写入更新变量,但此更新可能发生或您创建的任何线程发生,该怎么办。

你显然想保护那个变量,你可以使用关键字 @synchronized 为你创建锁,但这里有问题(摘自 Apple 文档)

The @synchronized() directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block.

这意味着如果您同步了一个对象并且两个线程同时写入它,即使是主线程也会阻塞,直到两个线程都完成写入数据。

展示所有这些的代码示例:

// Create the background queue
dispatch_queue_t queue = dispatch_queue_create("synchronized_example", NULL);

// Start working in new thread
dispatch_async(queue, ^
               {                   
                   // Synchronized that shared resource
                   @synchronized(sharedResource_)
                   {   
                       // Write things on that resource
                       // If more that one thread access this piece of code:
                       // all threads (even main thread) will block until task is completed.
                       [self writeComplexDataOnLocalFile];
                   }                         
               });

// won’t actually go away until queue is empty
dispatch_release(queue);

所以问题很简单:如何克服这个问题?我们如何才能安全地在除主线程之外的所有线程上添加锁,我们知道在这种情况下不需要被阻塞?

编辑澄清

正如你们中的一些人评论的那样,只有两个试图获取锁的线程应该阻塞,直到它们都完成为止,这似乎是合乎逻辑的(这显然是我最初在使用同步时的想法)。

然而,在实际情况下测试,似乎并非如此,主线程似乎也受到锁定的影响。

我使用这种机制在单独的线程中记录内容,这样 UI 就不会被阻塞。但是当我进行大量日志记录时,UI(主线程)显然受到了很大影响(滚动不那么流畅)。

所以这里有两个选择:要么后台任务太重,甚至主线程也会受到影响(我怀疑),要么 synchronized 在执行锁定操作时也会阻塞主线程(我正在开始重新考虑)。

我将使用 Time Profiler 进行更深入的挖掘。

最佳答案

我相信您误解了您从 Apple 文档中引用的以下句子:

Other threads are blocked until the thread exits the protected code...

这并不意味着所有线程都被阻塞,它只是意味着所有试图在同一对象(您的示例中的 _sharedResource)上进行同步的线程都被阻塞。

以下引述摘自 Apple 的 Thread Programming Guide ,这清楚地表明只有在同一对象上同步的线程才会被阻塞。

The object passed to the @synchronized directive is a unique identifier used to distinguish the protected block. If you execute the preceding method in two different threads, passing a different object for the anObj parameter on each thread, each would take its lock and continue processing without being blocked by the other. If you pass the same object in both cases, however, one of the threads would acquire the lock first and the other would block until the first thread completed the critical section.

更新:如果您的后台线程正在影响界面的性能,那么您可能需要考虑让后台线程休眠。这应该让主线程有时间更新 UI。

我知道你正在使用 GCD,但是,例如,NSThread 有几个方法可以挂起线程,例如-sleepForTimeInterval:。在 GCD 中,您可能只需调用 sleep()

或者,您可能还想考虑将线程优先级更改为较低的优先级。同样,NSThread 具有用于此目的的 setThreadPriority:。在 GCD 中,我相信您只会为分派(dispatch)的 block 使用低优先级队列。

关于ios - 有没有办法让 synchronized 关键字不阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10298762/

相关文章:

c# - 向线程发送消息的最佳方式

iphone - 变量的类型不完整 'struct sockaddr_in'

ios - 获取关键请求数据时出错: AVFoundationErrorDomain reason: Optional ("An unknown error occurred (-42650)")

ios - 如何在 Swift 中调整 TableView 标题的缩进?

java - Thread.sleep() 没有按预期触发

ios5 - 如何从 iOS 上的播放列表播放歌曲(仅歌曲)?

ios - 在 iOS 中触摸时更改按钮的字体颜色

ios - 如何使用 Swift 代码(NSFileManager)创建目录

ios - 将 TableView 数据导出到 CSV 文件

Java 多线程和安全发布