小编Chu*_*Lin的帖子

有没有更好的方法来处理 C++ 中共享内存的竞争条件?

我的应用程序中需要 IPC。我尝试过socket,但速度不够快。因此,我使用共享内存来做到这一点。为了同步通信,我使用内存中的标志并在 while 循环中检查它。它可以工作,但是轮询非常耗时,这使得它比套接字的解决方案慢。我的代码中共享内存的实现是shoom。我想在 Windows 和 Mac 上执行此操作,目前正在 Windows 上进行测试。完整的代码如下:

服务器:

#include "include/shoom.h"
#include <stdio.h>
#include <thread>

typedef struct _packet_ {
    uint8_t flag;
    int data;
} _packet;

int main()
{
    int data;
    _packet* ptr;
    shoom::Shm server("test", sizeof(_packet));

    if (server.Create() == shoom::kOK)
    {
        ptr = (_packet*)server.Data();

        while (1)
        {
            while (!ptr->flag)
            {
                std::this_thread::sleep_for(std::chrono::microseconds(100));
            }

            data = ptr->data;
            ptr->flag = 0;

            printf("%d\n", data);
        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

客户:

#include "include/shoom.h"
#include <stdio.h>
#include <chrono>
#include <thread>

typedef struct _packet_ …
Run Code Online (Sandbox Code Playgroud)

c++ synchronization shared-memory

5
推荐指数
0
解决办法
442
查看次数

标签 统计

c++ ×1

shared-memory ×1

synchronization ×1