请考虑以下情形:
要求:
程序序列(伪代码):
流程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)
现在这个古老的问题:如何有效地同步它们!? …
我最近发现了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)
我想知道会是什么
我试图配置模板,但我不确定这是否是最佳解决方案:
struct RecordKey
{
MainKey mainKey;
SecondaryKey secondaryKey;
RecordKey( const MainKey mainKey, SecondaryKey secondaryKey): …Run Code Online (Sandbox Code Playgroud) 这是我用来在共享内存上分配映射的一段代码,我使用的是 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 …