首页 \ 问答 \ 在两个进程之间同步访问共享内存中的数据[重复](Synchronized access to data in shared memory between two processes [duplicate])

在两个进程之间同步访问共享内存中的数据[重复](Synchronized access to data in shared memory between two processes [duplicate])

这个问题在这里已有答案:

我有两个进程在共享内存中有数据。 这些数据将通过这两个过程进行更新。 我正在寻找两个进程之间的一些锁定机制。 使用线程很容易拥有共享的互斥锁。 在我的例子中,我试图将互斥变量保存在共享内存中,然后两个进程都会使用它来锁定。 这不起作用。 如何在两个进程之间共享互斥锁。 有人说互斥量不能共享,使用信号量。 为什么互斥不能共享,但信号量可以?


This question already has an answer here:

I have two processes that have data in shared memory. This data is going to be updated by both of these process. I was looking for some locking mechanism between two processes. With threads it was easy to have a shared mutex lock. In my case, I tried to save the mutex variable in the shared memory, which then will be used by both processes for locking. This didn't work though. How do I share a mutex between two processes. Some say mutexes cannot be shared, use semaphores. Why mutex cannot be shared but semaphores can be?


原文:https://stackoverflow.com/questions/37890740
更新时间:2024-03-28 11:03

最满意答案

根据Core Data Programming Guide的“核心数据并发性”部分

推荐用于与Core Data并发编程的模式是线程限制:每个线程都必须拥有完全私有的受管对象上下文。

使用线程限制,您不应该在线程之间传递托管对象或托管对象上下文。

它看起来像你传递一个托管对象到后台线程,这是禁止的。 我不知道你是否也试图在线程之间共享你的托管对象上下文。

该文档描述了将托管对象传递给其他线程的几个解决方法。 你需要实现其中之一。


According to the "Concurrency with Core Data" section of Core Data Programming Guide:

The pattern recommended for concurrent programming with Core Data is thread confinement: each thread must have its own entirely private managed object context.

and

Using thread confinement, you should not pass managed objects or managed object contexts between threads.

It looks like you're passing a managed object to the background thread, which is forbidden. I don't know if you're also trying to share your managed object context between threads.

That document describes a couple of workarounds for passing managed objects to other threads. You'll need to implement one of them.

相关问答

更多
  • 首先,选择一个JSON库来使用,我个人喜欢TouchJSON,但还有其他几个也是非常好的。 复杂的部分虽然不是很难,但是将您的托管对象转换成适合的转换结构。 我写这个真正的快速,所以它可能有一个错误或两个:) 你所说的方法是: - (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects; - (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManaged ...
  • 你应该可以保存大师MOC的步伐。 没有意义的MOC等到结束才能保存。 它有自己的线程,它也有助于保持内存下降。 你写了: 然后在导入过程结束时,我保存主/父上下文,表面上,将修改推送到其他子上下文,包括主上下文: 在你的配置中,你有两个孩子(主要的MOC和背景MOC),都是父母的“主人”。 当你保存一个孩子时,它会将更改推送到父项中。 该MOC的其他子项将在下次执行提取时查看数据,但未明确通知。 所以当BG保存时,它的数据被推送到MASTER。 但请注意,除非MASTER保存,否则这些数据都不在磁盘上。 此 ...
  • 如果你仍然想要一个部分的名字。下面的代码工作: - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{ switch(type) { ...
  • 根据Core Data Programming Guide的“核心数据并发性”部分 : 推荐用于与Core Data并发编程的模式是线程限制:每个线程都必须拥有完全私有的受管对象上下文。 和 使用线程限制,您不应该在线程之间传递托管对象或托管对象上下文。 它看起来像你传递一个托管对象到后台线程,这是禁止的。 我不知道你是否也试图在线程之间共享你的托管对象上下文。 该文档描述了将托管对象传递给其他线程的几个解决方法。 你需要实现其中之一。 According to the "Concurrency with ...
  • 如果在每个秒或半秒内保存在您的场景中没有意义,那么在完成更新对象时保存。 如果您一直在ping服务器,请尝试保存当ping服务器时ping服务器或取消分配。 If saving every second or half a second does not make sense in you scenario then save when you are finished updating the object. If you are pinging the server all the time, try ...
  • 好吧,首先,尝试了解核心数据的工作原理。 我认为你非常接近,但编程就是要真正知道发生了什么,而不是'有点'知道发生了什么。 “ 核心数据入门”指南非常有启发性。 现在关于你的代码(我不知道你是精确的数据模型和周围的代码,所以我在这里猜测)。 我假设你有一个名为'datePicker'的成员,它引用你正在使用的日期选择器。 好吧,让我看看我是否可以快速修复代码中的一些内容,但要小心一点。 我没有阅读你的源代码的每一行,我还没有尝试编译它,所以可能仍然会有错误。 最后,您仍然可以彻底了解代码。 - (void ...
  • 核心数据的货币是NSSet*实例。 集合是无序的,所以你添加和取回的任何内容都会返回给你,没有任何固有的顺序。 所以当你初始化你的NSSortDescriptor时你只需要应用一个NSSortDescriptor 。 您可以根据需要应用尽可能多的排序顺序,例如: NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(ca ...
  • 这完全取决于您在数据库中执行的插入量。 我发现最好在后台线程中进行所有下载,然后在主线程中插入对象。 只要每30分钟只有几百个更新,你应该对UI性能的影响很好。 根据我的经验,这样做的健壮性和简单性远远超过对主线程的小的性能影响 - 您不必关心同步上下文,确保在正确的线程中使用正确的对象等。 It all depends on how many inserts you do into your database. I found it working best to do all the downloadi ...
  • 您的segmentOutlet需要指定为strong 。 内存系统在某个时候丢弃它; 然后,当你尝试self.segmentOutlet ,返回值为nil 。 在调用selectSegmentIndex失败之后,默认情况下,返回nil ,最终得到'null'。 尝试: @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentOutlet; Your segmentOutlet needs to be specified as st ...
  • 我开始用这个简单的教程学习核心数据: iOS 5上的核心数据教程:入门 iOS 5上的核心数据教程:如何使用NSFetchedResultsController I started to learn Core Data with this simple tutorial: Core Data on iOS 5 Tutorial: Getting Started Core Data on iOS 5 Tutorial: How To Use NSFetchedResultsController

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)