- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 linux 2.6 和位于 mmap 文件后备内存中的互斥量时遇到了一个奇怪的并发问题。它是一个闩锁管理器模块。页错误逻辑是否重新启动了一条完全包含锁定前缀的指令?它的行为就像它在重新启动时不包含锁定前缀一样。如果我在 mmap 调用中设置 MAP_LOCKED 属性,问题就会消失。有经验的人吗?
// mode & definition for hash latch implementation
enum {
Mutex = 1,
Write = 2,
Pending = 4,
Share = 8
} LockMode;
// mutex locks the other fields
// exclusive is set for write access
// share is count of read accessors
typedef struct {
volatile ushort mutex:1;
volatile ushort exclusive:1;
volatile ushort pending:1;
volatile ushort share:13;
} BtSpinLatch;
// wait for other read and write latches to relinquish
void bt_spinwritelock(BtSpinLatch *latch)
{
do {
#ifdef unix
while( __sync_fetch_and_or((ushort *)latch, Mutex | Pending) & Mutex )
sched_yield();
#else
while( _InterlockedOr16((ushort *)latch, Mutex | Pending) & Mutex )
SwitchToThread();
#endif
if( !(latch->share | latch->exclusive) ) {
#ifdef unix
__sync_fetch_and_or((ushort *)latch, Write);
__sync_fetch_and_and ((ushort *)latch, ~(Mutex | Pending));
#else
_InterlockedOr16((ushort *)latch, Write);
_InterlockedAnd16((ushort *)latch, ~(Mutex | Pending));
#endif
return;
}
#ifdef unix
__sync_fetch_and_and ((ushort *)latch, ~Mutex);
sched_yield();
#else
_InterlockedAnd16((ushort *)latch, ~Mutex);
SwitchToThread();
#endif
} while( 1 );
}
void bt_spinreadlock(BtSpinLatch *latch)
{
ushort prev;
do {
#ifdef unix
while( __sync_fetch_and_or((ushort *)latch, Mutex) & Mutex )
sched_yield();
#else
while( _InterlockedOr16((ushort *)latch, Mutex) & Mutex )
SwitchToThread();
#endif
// see if exclusive request is granted or pending
if( prev = !(latch->exclusive | latch->pending) )
#ifdef unix
__sync_fetch_and_add((ushort *)latch, Share);
#else
_InterlockedExchangeAdd16 ((ushort *)latch, Share);
#endif
#ifdef unix
__sync_fetch_and_and ((ushort *)latch, ~Mutex);
#else
_InterlockedAnd16((ushort *)latch, ~Mutex);
#endif
if( prev )
return;
#ifdef unix
} while( sched_yield(), 1 );
#else
} while( SwitchToThread(), 1 );
#endif
}
最佳答案
可能是页面错误引入的延迟暴露了否则看不到的竞争条件。
我建议从 pthread_spinlock_t
(它只是一个 int
)开始,并在实现您自己的自旋锁之前确保您的软件正常工作。错误可能出在其他地方。
该代码存在一些问题:
BtSpinLatch* latch
作为 BtSpinLatch
和 ushort
打破了导致未定义行为的别名规则。将 BtSpinLatch
设为 ushort
并坚持仅使用位掩码和移位访问位。pending
位,即使还有另一个 pending writer。__sync_fetch...
内置函数已过时且过于昂贵,因为它们构成了完整的内存屏障,并且在这里被调用的次数超过了必要的次数。使用 built-in functions for memory model aware atomic operations .关于c - Linux页面错误内存中的互斥锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21807231/
我只是有一个关于 Java 并发编程的简单问题。例如,我有一个 NxN 矩阵,矩阵的每一行都有一个对应的线程,如果每一行中的线程之间没有交互,如果多个线程同时访问和修改矩阵的单独行是否安全(或正确)?
jwplayer("myElement_0").onPlay(function(){ pausePlayers(1); alert('0 has started'); }); jwplayer("my
我在想。当我使用一个std::mutex _lock 并想依赖守卫来释放它时,我可以这样使用吗? class MyClass{ private: mutable std::mutex _loc
这种互斥模式是否像我认为的那样安全?如果是这样,你怎么调用它? lock (_lock) { if (_flag) return; else _flag = true; } try {
我有一个静态库 来访问数据库。它有一个函数 readMaximum()。 readMaximum() 从数据库中读取最大值。此函数是线程安全的(使用互斥量)。 但问题是: 有两个进程A.exe和B.e
我正在为互斥锁的逻辑而苦苦挣扎;我在这里检查 key 是否被拿走,如果没有,我们拿走它,完成后释放它;但是你能帮我看看我怎样才能有一个循环来检查 key 直到它可用吗? rdb.setnx(lockk
我正在使用 clp(fd) 编写 Prolog 程序并且我很难实现我想要的约束之一。 输出是一个整数列表(长度取决于程序另一部分的输入),其中有某些相互排斥的预定义数字对,每对中的一个数字必须在输出中
我正在尝试使用 mesos/marathon 实现一个 lizardfs 集群。为此,我创建了一些 docker 镜像。 我需要获取某种类型的 docker 容器(lizardfs-master),而
假设我有一个包含以下列的 MySQL 表: |variables|1. | 'a' |2. | 'a b' |3. | 'a b c' |4. | ... | How can
我在一个表单上有 7 个单选按钮 其中3个属于一个组,4个属于另一个组 我希望 RadioButtons 在组内互斥。 这可以通过将它们放在两个 GroupBox 中来实现, 有没有什么办法可以不把它
我正在尝试使文本输入和复选框相互排斥。 默认情况下,文本框应包含“0”,复选框应为 false。如果用户选中该复选框,则该文本框应被清空。如果用户在文本框中输入值,则应取消选中该复选框。任何时候都不应
我已经在代码的注释中写下了我的问题。 我正在尝试使用 pthreads 使我的代码并行。首先,我想通过多个线程并行地在内存中写入大量数据。写入数据后,我想通过相同的线程执行此数据。执行数据后,我想读取
这个问题在这里已经有了答案: 关闭 13 年前。 Possible Duplicate: Monitor vs Mutex in c# 你好, 在这个网站上,我发现来自不同人的不同答案令人困惑。仍然
我需要调用一个返回唯一 id 的函数, int getid() { static id=0; id++; return id; } 多个线程需要调用这个函数,我的问题是我不确定我需要在哪里锁定互斥量,
请引用来自 WWDC 的视频 https://developer.apple.com/videos/play/wwdc2015/226/演讲者展示了我们可以在两个相同类型的 NSopeation 实例
概述: 我有一个使用 CoreData 的 iOS 应用程序> 有一个名为Animal的实体 它有一组记录Lion、Tiger和Elephant 我想将只有一个记录标记为收藏。 具有相同方法的相似实体
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: DLL thread safety 你好 我正在 MS VS C++ express 中编写一个 DLL 文
我的应用程序被迫使用第 3 方模块,如果在同一台机器上同时启动两个实例,该模块将使 Windows 蓝屏。为了解决这个问题,我的 C# 应用程序有一个互斥量: static Mutex mutex =
我是 Go 的新手,我想实现一个自定义的互斥机制,每个应用程序用户一次可以执行一个 goroutine。为简化起见,将 U1 和 U2 视为应用程序用户以及 F1(userId)、F2(userId)
我需要将单选和多选的功能组合到一个控件中。具体来说,我有多种选择。第一个与其他的互斥。所以,如果我选择第一个,就需要取消选中所有其他的。如果选择了其中一个,则必须取消选中第一个(如果已选择)。其他选项
我是一名优秀的程序员,十分优秀!