gpt4 book ai didi

c++ - 在OSX上32和64位程序之间的共享内存中的boost::interprocess同步机制(互斥量,条件)

转载 作者:行者123 更新时间:2023-12-01 14:50:39 25 4
gpt4 key购买 nike

OSX 10.11.6,提升1.63.0

我有一个64位的进程,它使用32位的从属进程进行实时渲染(我没有特定32位动态库的源代码,因此无法将其重新编译为64位)。时间是至关重要的,我发现boost::interprocess的共享内存实用程序可以很好地工作,但是我遇到了互操作性问题。当编译为32 vs 64时,某些进程间机制的大小不同,因此,当从属从共享内存中引用它们时,会引起问题。

64 bit
Scoped Lock: 16
Condition: 48
Mutex: 64
Semaphore: 4

32 bit
Scoped Lock: 8
Condition: 28
Mutex: 44
Semaphore: 4

我深入研究boost::interprocess::interprocess_mutex header ,发现在 _pthread_types.h 中声明的大小:
// pthread opaque structures
#if defined(__LP64__)
#define __PTHREAD_SIZE__ 8176
#define __PTHREAD_ATTR_SIZE__ 56
#define __PTHREAD_MUTEXATTR_SIZE__ 8
#define __PTHREAD_MUTEX_SIZE__ 56
#define __PTHREAD_CONDATTR_SIZE__ 8
#define __PTHREAD_COND_SIZE__ 40
#define __PTHREAD_ONCE_SIZE__ 8
#define __PTHREAD_RWLOCK_SIZE__ 192
#define __PTHREAD_RWLOCKATTR_SIZE__ 16
#else // !__LP64__
#define __PTHREAD_SIZE__ 4088
#define __PTHREAD_ATTR_SIZE__ 36
#define __PTHREAD_MUTEXATTR_SIZE__ 8
#define __PTHREAD_MUTEX_SIZE__ 40
#define __PTHREAD_CONDATTR_SIZE__ 4
#define __PTHREAD_COND_SIZE__ 24
#define __PTHREAD_ONCE_SIZE__ 4
#define __PTHREAD_RWLOCK_SIZE__ 124
#define __PTHREAD_RWLOCKATTR_SIZE__ 12
#endif // !__LP64__

struct _opaque_pthread_mutex_t {
long __sig;
char __opaque[__PTHREAD_MUTEX_SIZE__];
};

使用时
boost::interprocess::interprocess_mutex
boost::interprocess::interprocess_condition

两种程序均编译为64位,因此数据呈现效果非常好。

有什么办法可以迫使尺寸保持一致?还是有我忽略的其他IPC机制?

使用条件的好处在于,它们可以在何时唤醒时向阻塞线程发出信号,并最大程度地减少浪费的CPU周期,因为等待线程/进程不必一直运行来检查值。还有另一种方式以这种方式向线程/进程发出信号吗?

我知道OSX上至少有几个程序在64位<-> 32位之间实现了快速高效的数据渲染,因此答案一定在那儿。

最佳答案

因此,我接受了MikeMB的建议,并对管道进行了一些研究。我发现本文http://ace.ucv.ro/sintes12/SINTES12_2005/SOFTWARE%20ENGINEERING/44.pdf建议使用管道作为锁定机制。看起来,对管道的读/写实现了进程间条件变量的相同信令机制,因此线程阻塞/唤醒非常有效。

看来读写速度足够快,可以在我的机器上进行实时设置! (尽管def不能保证可以在其他计算机上使用,但我仅在2011 MBP(2.2 GHz Intel Core i7)上进行过测试)

它看起来像条件​​一样好,并且我还没有丢失任何数据。这就是我做的。只需将其中一个放置在共享内存中,从属进程就可以相应地调用wait()和post()。

struct PipeSemaphore
{
PipeSemaphore()
{
// make sure semaphore will work in shared memory
// between 32 <-> 64 bit processes
static_assert (sizeof (int) == 4);

int res = pipe (p);

// pipe failed to open
if (res == -1)
assert (false);
}

~PipeSemaphore()
{
close (p[0]);
close (p[1]);
}

// if nothing is in the pipe stream,
// the curr thread will block
inline int wait() noexcept
{
char b;
return read (p[0], &b, 1);
}

// writing to the pipe stream will notify
// & wake blocked threads
inline int post() noexcept
{
return write (p[1], "1", 1);
}

private:
int p[2];
};

对代码的任何建议将不胜感激!

关于c++ - 在OSX上32和64位程序之间的共享内存中的boost::interprocess同步机制(互斥量,条件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775504/

25 4 0