- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在愤怒中学习和使用增强共享内存一段时间后,我得出了一个心智模型,即何时使用哪种类型的互斥体,看起来像这样:
class IntendedToResideInProcessMemory {
boost::mutex mutex_for_this_process; // 1
boost::interprocess::interprocess_mutex
ok_but_pointless_to_use_interprocess_mutex_here; // 2
}
class IntendedToBeCreatedInSharedMemory {
boost::mutex bad_mutex_at_a_guess_this_will_allocate_something_on_heap;// 3
boost::interprocess::interprocess_mutex
good_for_multiprocess_shared_lock; // 4
}
我希望原则上我没有弄错,所以请纠正我。我的意图是向前推进并更正现有代码以及我自己的不符合此图的代码,但我想确定。
这个问题分为两部分:我认为它确实没问题,但使用//2 毫无意义 - 在上下文中这不如//1,但原因是什么?性能?
对于//3,我猜对了吗?有人可以给出它不起作用的技术幕后原因,或者至少在什么情况下它不起作用?
最佳答案
关于第 3 点:您的 boost::mutex
对您的其他进程不可见。进程间互斥使用系统全局命名的对象,可以被另一个进程访问,据我所知,boost::mutex
没有。
你的 boost::mutex
在共享内存中创建,例如在 Windows 中,有一个 HANDLE
到一个 windows Mutex 对象,它将分配在你的进程的私有(private)堆。该堆对其他进程不可见。
如果您查看 boost::mutex
类,它有这个 typedef
typedef platform-specific-type native_handle_type;
这是特定于平台的。
另一个例子:POSIX 互斥锁由它们的地址标识,进程之间是不同的感谢@DeadMG 这个例子:)
关于c++ - 与 boost::interprocess_mutex 相比,为什么不在共享内存中使用 boost::mutex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051834/
我需要在一个函数内锁定一个 std::map 和两个 boost::multimaps 的操作,因为我们有线程试图访问该函数(以及映射)。 我计划使用“std::mutex mutex_var”来保护
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
或不同的标题: 为什么处置获得的 Mutex 会破坏它? 我有以下代码,真正的代码在几个方法之间产生,并在这个方法休眠的地方做事: bool createdNew; u
如何测量互斥量、信号量或 futex 的延迟?我的意思是两个事件之间的延迟:解锁先前锁定的互斥体和锁定该互斥体。有两种情况:当所有线程/进程都在同一个 CPU 上时(重新调度线程需要多长时间)以及当第
我执行了以下程序,其中我创建了 100 个线程并发执行。请注意这是一个示例程序。我知道下面的程序不需要多线程,但我的目的是测试互斥量。 class ThreadPool{ public:
我有创建多个线程的代码,所有线程都尝试将信息记录在一个文件中我尝试使用互斥锁来登录文件,但是当我使用 Mutex() 和 Mutex(true or false, "name") 对象时,我得到了不同
我正在研究 Rust 示例。有这段代码: fn new(name: &str, left: usize, right: usize) -> Philosopher { Philosopher
我正在实现一个基于 std::queue 的 C++ 消息队列。 因为我需要 popers 在空队列上等待,所以我考虑使用 mutex 进行互斥,并使用 cond 在空队列上挂起线程,就像 glib
在golang中,sync.Mutex Lock和Unlock是usaul操作,但是Lock和defer Unlock的正确顺序是什么? mu.Lock() defer mu.Unlock() 或 d
在 Go 中,我们可以使用: type Data struct { lock *sync.Mutex } 或 type Data struct { lock sync.Mutex
我尝试摆脱代码中的一些 boost 依赖项,转而使用新的 C++11 功能 (Visual Studio 2013)。 在我的一个组件中,我使用了 boost::mutex与 boost::lock_
我正在使用 scoped_lock 和 mutex 来实现 BlockingQueue posted in a different SO question 的一个版本, 但在 boost 中有多个不同
我在互斥锁析构函数中遇到了上述错误。由于错误可能是由于互斥锁在销毁过程中处于锁定状态,所以我创建了一个新的互斥锁类,它继承自 boost:mutex。这是为了确保互斥锁在销毁期间解锁。但是,仍然会出现
今天写了一些代码来测试mutex的性能。 这是 boost(1.54) 版本,在 vs2010 上编译并进行了 O2 优化: boost::mutex m; auto start = boost::c
我不知道我没有做什么,但我根本无法让自己的调试器保存正在调试的应用程序的“Mutex Owned”或“Mutex Free”信息。 如果我按如下方式调用它,CDB 就可以正常工作: cdb -pn
还没有网上的例子来生动地演示这一点。在 http://en.cppreference.com/w/cpp/header/shared_mutex 看到了一个例子但目前还不清楚。有人可以帮忙吗? 最佳答
我有两个用例。 A.我想同步访问两个线程的队列。 B.我想同步两个线程对队列的访问并使用条件变量,因为其中一个线程将等待另一个线程将内容存储到队列中。 对于用例 A,我看到了使用 std::lock_
我编写了一个小型 Go 库 ( go-patan ),用于收集某些变量的运行最小值/最大值/平均值/标准偏差。我将它与等效的 Java 实现 ( patan ) 进行了比较,令我惊讶的是 Java 实
我想知道这两者之间的区别是什么 boost::timed_mutex _mutex; if(_mutex.timed_lock(boost::get_system_time() + boost::po
我正在尝试将 dyn 特征存储在 Arc>>>> 中,但是由于某种原因它不起作用 use std::sync::{Arc, Mutex}; trait A{} struct B{} impl A fo
我是一名优秀的程序员,十分优秀!