简体   繁体   English

存储在共享内存中的C ++对象(volatile成员函数?)

[英]C++ object stored in shared memory (volatile member function?)

I want to know what is a good practice in using objects stored in shared memory. 我想知道使用存储在共享内存中的对象的好习惯。 The options I have in my mind are: 我心中的选择是:

  1. Add volatile to every member functions of the objects stored in shared memory 将volatile添加到存储在共享内存中的对象的每个成员函数
  2. Copy the entire data from/to shared memory at every iteration. 在每次迭代时从/向共享内存复制整个数据。
  3. Access shared memory without volatile. 访问没有volatile的共享内存。

Let me explain the problem that I have: 让我解释一下我遇到的问题:

I have two processes running on Linux on FPGA. 我在Linux上运行了两个进程。 They communicates data via the shared memory. 它们通过共享内存传输数据。 Since they lock each other by binary semaphore, only one process do its job at a time. 由于它们通过二进制信号量相互锁定,因此一次只能有一个进程完成其工作。 Compiler is g++ 3.4.x. 编译器是g ++ 3.4.x. My current code is something like below: 我目前的代码如下所示:

struct MyTime
{
  int32 dayNumber;
  int32 milliSecOfDay;
  void convert(double* answer);
};
struct MyData
{
  double var1;
  MyTime time;
};
volatile MyData* ptr;
ptr = (volatile MyData*)shmat(shmid, NULL, 0);

double answer;
ptr->time.convert(&answer);  //< ERROR(*)

*: error: passing const volatile TimeTTJ2000' as this' argument of `bool TimeTTJ2000::get_Tu_UT1(double&, const int32&, const int32&) const' discards qualifiers *:错误:传递const volatile TimeTTJ2000' as `bool的'参数TimeTTJ2000 :: get_Tu_UT1(double&,const int32&,const int32&)const'丢弃限定符

(The above code is just made up for explanation. The error message is from my real code, in which the size of MyData is much larger.) (以上代码仅用于解释。错误消息来自我的真实代码,其中MyData的大小要大得多。)

To remove that error, it seems to me that I would have to define another member function like 要删除该错误,在我看来,我将不得不定义另一个成员函数

MyTime::convert(double* answer) volatile;

But it seems to me ridiculous that I have to add 'volatile' to all the functions in the libraries that are not necessarily mine. 但在我看来,我必须将“易变”添加到库中不一定是我的所有函数中,这似乎是荒谬的。

To avoid having 'volatile' everywhere, I think I can copy the entire data in shared memory to local right after one process is unlocked, and write back to shared memory right before the process is locked. 为了避免在任何地方出现“volatile”,我想我可以在一个进程解锁后立即将共享内存中的整个数据复制到本地,并在进程被锁定之前写回共享内存。 By this way, I am not bothered with volatile, but still is this wise thing to do? 通过这种方式,我不会感到烦恼,但这仍然是明智之举吗?

Or can I access shared memory data without using volatile in first place? 或者我可以在不使用volatile的情况下访问共享内存数据吗? That would make my life easier. 这会让我的生活更轻松。 (I have little experience in shared memory and volatile. I'm not very certain when volatile is needed. Of course I know basics like, volatile suppresses optimization.) (我对共享内存和易失性方面的经验很少。我不太确定什么时候需要volatile。当然我知道基本知识,例如,volatile会抑制优化。)

But it seems to me ridiculous that I have to add 'volatile' to all the functions in the libraries that are not necessarily mine. 但在我看来,我必须将“易变”添加到库中不一定是我的所有函数中,这似乎是荒谬的。

That is what the c++ standard says it should be done. 这就是c ++标准所说应该完成的。 You can cast away the const/volatile specifier, but by doing that, you may introduce UB. 您可以抛弃const / volatile说明符,但通过这样做,您可以引入UB。

Or can I access shared memory data without using volatile in first place? 或者我可以在不使用volatile的情况下访问共享内存数据吗?

Yes, you do not need volatile to access shared memory. 是的,您不需要volatile来访问共享内存。 And since you are locking the access with a semaphore, you do not need to copy data. 由于您使用信号量锁定访问权限,因此无需复制数据。

You would need volatile in a case your FPGA writes to some memory (which is not shared memory). 在FPGA写入某些内存(不是共享内存)的情况下,您需要使用volatile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM