@synchronized 是否保证线程安全? [英] Does @synchronized guarantees for thread safety or not?

查看:25
本文介绍了@synchronized 是否保证线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此答案,我想知道这是正确的吗?

With reference to this answer, I am wondering is this correct?

@synchronized 不会使任何代码线程安全"

@synchronized does not make any code "thread-safe"

因为我试图找到任何文档或链接来支持这个声明,但没有成功.

As I tried to find any documentation or link to support this statement, for no success.

对此的任何评论和/或答案将不胜感激.

Any comments and/or answers will be appreciated on this.

为了更好的线程安全,我们可以使用其他工具,这是我知道的.

推荐答案

@synchronized 如果使用得当,确实可以使代码线程安全.

@synchronized does make code thread safe if it is used properly.

例如:

假设我有一个访问非线程安全数据库的类.我不想同时读取和写入数据库,因为这可能会导致崩溃.

Lets say I have a class that accesses a non thread safe database. I don't want to read and write to the database at the same time as this will likely result in a crash.

所以可以说我有两种方法.storeData: 和 readData 在一个名为 LocalStore 的单例类上.

So lets say I have two methods. storeData: and readData on a singleton class called LocalStore.

- (void)storeData:(NSData *)data
 {
      [self writeDataToDisk:data];
 }

 - (NSData *)readData
 {
     return [self readDataFromDisk];
 }

现在,如果我像这样将这些方法中的每一个分派到它们自己的线程上:

Now If I were to dispatch each of these methods onto their own thread like so:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      [[LocalStore sharedStore] storeData:data];
 });
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      [[LocalStore sharedStore] readData];
 });

我们可能会崩溃.但是,如果我们将 storeData 和 readData 方法更改为使用 @synchronized

Chances are we would get a crash. However if we change our storeData and readData methods to use @synchronized

 - (void)storeData:(NSData *)data
 {
     @synchronized(self) {
       [self writeDataToDisk:data];
     }
 }

 - (NSData *)readData
 { 
     @synchronized(self) {
      return [self readDataFromDisk];
     }
 }

现在这段代码将是线程安全的.重要的是要注意,如果我删除 @synchronized 语句之一,则代码将不再是线程安全的.或者,如果我要同步不同的对象而不是 self.

Now this code would be thread safe. It is important to note that if I remove one of the @synchronized statements however the code would no longer be thread safe. Or if I were to synchronize different objects instead of self.

@synchronized 在您要同步的对象上创建互斥锁.所以换句话说,如果任何代码想要访问 @synchronized(self) { } 块中的代码,它必须排在同一块中运行的所有先前代码之后.

@synchronized creates a mutex lock on the object you are syncrhonizing. So in other words if any code wants to access code in a @synchronized(self) { } block it will have to get in line behind all previous code running within in that same block.

如果我们要创建不同的 localStore 对象,@synchronized(self) 只会单独锁定每个对象.有意义吗?

If we were to create different localStore objects, the @synchronized(self) would only lock down each object individually. Does that make sense?

这样想.你有一大群人在不同的线等待,每条线编号为 1-10.您可以选择您希望每个人在哪一行等待(通过在每行基础上同步),或者如果您不使用 @synchronized,您可以直接跳到前面并跳过所有行.第 1 行的人不必等待第 2 行的人说完,但第 1 行的人确实必须等待他们排在他们前面的每个人都说完.

Think of it like this. You have a whole bunch of people waiting in separate lines, each line is numbered 1-10. You can choose what line you want each person to wait in (by synchronizing on a per line basis), or if you don't use @synchronized you can jump straight to the front and skip all the lines. A person in line 1 doesn't have to wait for a person in line 2 to finish, but the person in line 1 does have to wait for everyone in front of them in their line to finish.

这篇关于@synchronized 是否保证线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆