在两个进程之间同步访问共享内存中的数据[重复](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
最满意答案
根据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保存,否则这些数据都不在磁盘上。 此 ...
-
iPhone:核心数据保存错误(iPhone: Core Data saving error)[2023-06-06]
如果你仍然想要一个部分的名字。下面的代码工作: - (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 ...
-
iPhone核心数据一次保存多个项目的随机行为?(iPhone Core Data saving multiple items at once random behavior?)[2023-08-21]
核心数据的货币是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