简体   繁体   English

在共享内存上分配原子

[英]Allocating a atomic on shared memory

I am trying to allocate a atomic on a shared memory block (on linux). 我正在尝试在共享内存块上分配原子(在Linux上)。 The atomic will be accessed and modified my multiple threads simultaneously. 原子将被同时访问并修改我的多个线程。 The rational behind allocating it on shared memory is because I want to persist the values so if my process is restarted the previous state can be recovered. 在共享内存上分配它的原因是因为我想保留这些值,因此,如果我的进程重新启动,则可以恢复以前的状态。 I know for a fact if i use a mutex in shared memory i have to initialize it as a SHARED. 我知道一个事实,如果我在共享内存中使用互斥锁,则必须将其初始化为SHARED。 Is there any such requirement for atomics? 对原子有这样的要求吗? Is this feasible? 这可行吗?

Yes, you can do that. 是的,你可以这么做。 Here is an example I ripped from Quora ( ripped code from Quora ), not my code and includes boost so I have not tested it: 这是我从Quora 翻录的一个示例(从Quora 翻录的代码 ),而不是我的代码,包括boost,所以我没有对其进行测试:

#include <atomic>
#include <string>
#include <iostream>
#include <cstdlib>
#include <boost/interprocess/managed_shared_memory.hpp>

using namespace std::string_literals;
namespace bip = boost::interprocess;
static_assert(ATOMIC_INT_LOCK_FREE == 2,
              "atomic_int must be lock-free");
int main(int argc, char *argv[])
{
  if(argc == 1) //Parent process
  {
    struct shm_remove {
      shm_remove() { bip::shared_memory_object::remove("szMem");}
      ~shm_remove(){ bip::shared_memory_object::remove("szMem");}
    } remover;
    bip::managed_shared_memory segment(bip::create_only,
                                       "szMem", 65536);
    auto ap = segment.construct<std::atomic_int>("the counter")(0);
    //Launch 5 child processes
    std::string s = argv[0] +" child"s;
    std::system((s + '&' + s + '&' + s + '&' + s + '&' + s).c_str());
    std::cout << "parent existing: counter = " << *ap << '\n';
    segment.destroy<std::atomic_int>("the counter");
 } else { // child
    bip::managed_shared_memory segment(bip::open_only, "szMem");
    auto res = segment.find<std::atomic_int>("the counter");
    for(int n = 0; n < 100000; ++n)
        ++*res.first; // C++17 will remove the dumb "first"
    std::cout << "child exiting, counter = " << *res.first << '\n';
  }
}

Here is documentation: link to Boost docs 这里是文档: 链接到Boost docs

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

相关问题 分配共享内存 - allocating shared memory 对共享内存的原子访问 - Atomic access to shared memory 跨共享库边界分配和解除分配 memory - Allocating and Deallocating memory across shared lib boundaries 使用 boost::interprocess 在共享内存中分配用户定义的结构 - Allocating a user defined struct in shared memory with boost::interprocess 在没有Volatile,std :: atomic,semaphore,mutex和spinlock的情况下访问共享内存? - Accessing Shared Memory Without Volatile, std::atomic, semaphore, mutex, and spinlock? 在这种特殊情况下,我是否需要共享内存中的原子类型? - Do I need an atomic type in shared memory in this particular case? C / C ++共享内存,原子操作,Linux - C/C++ shared memory, atomic operations, Linux 我是否在这里正确使用 atomic&lt;&gt; over shared memory - Do I use atomic<> over shared memory correctly here 我可以放置一个 std::atomic<int64> 在共享内存中并期望原子操作? - Can I place a std::atomic<int64> in shared memory and expect atomic operation? 我可以使用 C++20 `std::atomic<t> ::wait()` 或 `std::atomic_flag::wait()` 在共享 memory 中?</t> - Can I use C++20 `std::atomic<T>::wait()` or `std::atomic_flag::wait()` in shared memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM