相关疑难解决方法(0)

共享内存IPC同步(无锁)

请考虑以下情形:

要求:

  • Intel x64 Server(多个CPU插槽=> NUMA)
  • Ubuntu 12,GCC 4.6
  • 两个进程在(命名)共享内存上共享大量数据
  • 古典生产者 - 消费者情景
  • 内存安排在循环缓冲区(带M个元素)

程序序列(伪代码):

流程A(生产者):

int bufferPos = 0;
while( true )
{
    if( isBufferEmpty( bufferPos ) )
    {
        writeData( bufferPos );
        setBufferFull( bufferPos );

        bufferPos = ( bufferPos + 1 ) % M;
    }
}
Run Code Online (Sandbox Code Playgroud)

流程B(消费者):

int bufferPos = 0;
while( true )
{
    if( isBufferFull( bufferPos ) )
    {
        readData( bufferPos );
        setBufferEmpty( bufferPos );

        bufferPos = ( bufferPos + 1 ) % M;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这个古老的问题:如何有效地同步它们!? …

c++ synchronization ipc shared-memory lock-free

12
推荐指数
1
解决办法
1万
查看次数

基于std :: unordered_map(地图图)提升多索引容器与多级映射容器

我最近发现了boost :: multi_index_container,我对他的性能感到好奇,与我自己实现的基于多级映射的类似容器相比,定义如下:

typedef int      Data;
typedef uint64_t MainKey;
typedef uint64_t SecondaryKey;

typedef std::unordered_map<SecondaryKey, Data>       SecondaryMap;
typedef std::unordered_map<PrimaryKey, SecondaryMap> PrimaryMap;
Run Code Online (Sandbox Code Playgroud)

密钥排序并不重要.快速查找很重要,为此我使用的是:

// find primaryKey=10 and secondaryKey=30
PrimaryMap m;
....
auto i1 = m.find( 10);
if ( i1 != m.end())
{
    auto& secondary = i1->second;
    auto i2 = secondary.find( 30);
    if ( i2 != secondary.end())
    {
        found = true;
        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道会是什么

  • boost :: multi_index_container最接近的配置,以匹配我的实现
  • 按主键和辅助键搜索的最快方法.

我试图配置模板,但我不确定这是否是最佳解决方案:

struct RecordKey
{
    MainKey         mainKey;
    SecondaryKey    secondaryKey;

    RecordKey( const MainKey mainKey, SecondaryKey secondaryKey): …
Run Code Online (Sandbox Code Playgroud)

c++ boost unordered-map std boost-multi-index

7
推荐指数
2
解决办法
7147
查看次数

提升进程间和valgrind

这是我用来在共享内存上分配映射的一段代码,我使用的是 boost::interprocess 和托管共享内存段,现在的问题是我遇到了内存泄漏。下面给出的是顶部输出。

顶部输出:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
1.  27594 tpmon     20   0 46132 2140 1664 S  0.0  0.0   0:00.00 test_stub
2.  27594 tpmon     20   0 46132 2176 1664 S  0.0  0.0   0:00.01 test_stub
3.  27594 tpmon     20   0 46264 2248 1664 S  0.0  0.0   0:00.01 test_stub
4.  27594 tpmon     20   0 46264 2280 1664 S  0.0  0.0   0:00.01 test_stub
Run Code Online (Sandbox Code Playgroud)

从顶部输出可以明显看出常驻内存在不断增加,在共享内存映射中,我只有下面列出的条目,作为三元组:

Deb0 0 150520 DEB1 1 150520 Deb10 10 150520 Deb11 11 …

c++ memory boost valgrind

2
推荐指数
1
解决办法
562
查看次数