c ++ 11进程间原子和互斥体 [英] c++11 interprocess atomics and mutexes

查看:118
本文介绍了c ++ 11进程间原子和互斥体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Linux程序,该程序产生多个进程(fork)并通过POSIX共享内存进行通信。我想让每个进程分配一个ID(0-255)。我的意图是在共享内存区域中放置一个位向量(初始化为零),并进行原子比较并交换一位以分配ID。

I have a Linux program which spawns several processes (fork) and communicates through POSIX Shared Memory. I'd like to have each process allocate an id (0-255). My intention is to place a bitvector in the shared memory region (initialized to zero) and atomically compare and swap a bit to allocate an id.

是否有c ++ 11种友好的方式做到这一点?我可以创建原子位集吗?我可以跨进程使用互斥吗?如何确保构造函数在所有进程中一次只能调用一次?

Is there a c++11-friendly way to do this? Can I create an atomic bitset? Can I use a mutex across processes? How do I assure that constructors get called once and only once across all processes?

推荐答案

C ++ 11线程原语(互斥体,原子等)是 threading 原语。他们对进程一无所知,也不是实现进程间通信的手段。

C++11 threading primitives (mutexes, atomics, etc) are threading primitives. They know nothing about processes, and they are not a means of achieving inter-process communication.

由于C ++ 11标准没有提及进程或进程间在通信过程中,此类原语放在进程共享内存(内存映射文件,某种进程外全局映射内存等)中的行为是不确定的。

As the C++11 standard makes no mention of processes or inter-process communication, the behavior of such primitives when placed in process-shared memory (a memory-mapped file, some kind of out-of-process globally mapped memory, etc) is undefined.

这篇关于c ++ 11进程间原子和互斥体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆