Qt QSharedMemory如何更新共享内存位置
Qt QSharedMemory How to Update Shared Memory Location
我有一个库(testlib-lib.so),它由两个不同的Qt应用程序链接。我的问题是,我无法在初始化后更新共享内存。
void TestLib::createInitialSharedMemory() {
// Create shared memory
qDebug() << "Creating new shared memory...";
sharedAudioMemory.setKey("globalAudioBufferSharedMemory");
// First, test whether a shared memory segment is already attached to the process.
// If so, detach it
if (sharedAudioMemory.isAttached())
{
sharedAudioMemory.detach();
}
// Try to create memory of our required size
int sizeOfSharedData = 60 * 8000 * 2; // 60 Seconds x 8 KHz * 2 Bytes ( signed short )
if ( !sharedAudioMemory.create( sizeOfSharedData ) )
{
qDebug() << "ERROR: Failed to Allocate Shared Memory of size: " << sizeOfSharedData;
}
}
void TestLib::writeToSharedMemory( QString text ) {
// Create a buffer and data stream and some text to shove in there
QBuffer buffer;
buffer.open( QBuffer::ReadWrite );
QDataStream out( &buffer );
out << text;
int size = buffer.size();
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "Cannot attach to shared memory to update!";
}
// Write into the shared memory
sharedAudioMemory.lock();
qDebug() << "Writing data to buffer: " << text;
char *to = (char*)sharedAudioMemory.data();
const char *from = buffer.data().data();
memcpy( to, from, qMin( sharedAudioMemory.size(), size ) );
sharedAudioMemory.unlock();
}
Qt应用程序A的main()中有以下内容:
// Create the shared memory and load some data
TestLib loadData;
loadData.createInitialSharedMemory();
loadData.writeToSharedMemory("12345 init shared memory");
// This is the key
QSharedMemory sharedAudioMemory("globalAudioBufferSharedMemory");
// Attempt to attach to shared memory for the audio buffer
if (!sharedAudioMemory.attach()) {
//If an attempt of reading from the shared memory before data is written
qDebug() << "ERROR: Failed to attach to shared memory...";
return -1;
}
// Define a buffer to read the data in
QBuffer buffer;
QDataStream in(&buffer);
QString text;
// Lock the memory and read the data
sharedAudioMemory.lock();
char* pointer = (char*)sharedAudioMemory.constData();
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data.
buffer.open(QBuffer::ReadOnly);
in >> text;
sharedAudioMemory.unlock();
sharedAudioMemory.detach();
Qt应用程序B,尝试通过以下操作更新共享内存:
void Control::updateText() {
// Create seed for the random
// That is needed only once on application startup
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
// Get random value between 0-100
int randomValue = qrand() % ((1000 + 1) - 2) + 2;
// Create the shared memory and load some data
TestLib loadData;
QString randomText = QString("%1 test asdf %2 test asdf %3 test asdf %4").arg(randomValue).arg(randomValue).arg(randomValue).arg(randomValue);
loadData.writeToSharedMemory(randomText);
}
在将共享内存初始化为"12345init shared memory"之后,我似乎无法更新那里的文本存储。当我在App B中调用updateText()方法时,会调用库方法writeToSharedMemory(),该方法中的qDebug()会打印我希望在共享内存中更新的文本,但当我在尝试更新共享内存后调用以下代码时,它总是打印原始文本"12345 init shared memory":
// Attempt to attach to shared memory for the audio buffer
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "ERROR: Failed to attach to shared memory...";
}
// Define a buffer to read the data in
QBuffer buffer;
QDataStream in(&buffer);
QString text;
// Lock the memory and read the data
sharedAudioMemory.lock();
char* pointer = (char*)sharedAudioMemory.constData();
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data.
buffer.open(QBuffer::ReadOnly);
in >> text;
sharedAudioMemory.unlock();
sharedAudioMemory.detach();
// Print the data
qDebug() << "SHARED TEXT IS: " << text;
为什么我似乎不能更新共享内存?我做错了什么?谢谢-
我在这里打印出'errorString()'后发现了错误:
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "Cannot attach to shared memory to update! ERROR: " << sharedAudioMemory.errorString();
}
没有关联的密钥,因此它没有分离。所以我把这个功能改成:
// Assign the key so we can update the data
sharedAudioMemory.setKey("globalAudioBufferSharedMemory");
if (!sharedAudioMemory.attach()) {
// If an attempt of reading from the shared memory before data is written
qDebug() << "Cannot attach to shared memory to update! ERROR: " << sharedAudioMemory.errorString();
}
它很有效——我可以很好地修改共享内存。
相关文章:
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 字符串共享内存映射的向量
- CUDA 使用共享内存平铺 3D 卷积实现
- 共享内存:MapViewOfFile 返回错误 5
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- 有没有办法列出所有共享内存对象的名称?
- 共享内存的升压容器是否实现锁定?
- 共享内存中的健壮互斥锁不是那么健壮
- 使用IPC/共享内存将Integer数组从C++传递到Python
- 共享内存和性能
- 在这种特殊情况下,我是否需要在共享内存中使用原子类型
- 是否可以在专用内存空间中分配一个为提升管理共享内存而创建的对象
- fork(),在C中共享内存和指针
- 访问共享内存而不使用易失性、std::atomic、信号量、互斥锁和自旋锁
- 提升进程间共享内存open_or_create每次都会引发异常
- 通过 mmap-ed 共享内存传递可变长度 C 字符串
- 越界访问 CUDA 共享内存
- 在共享内存中插入映射映射时出现编译器错误
- 矩阵矢量产品 CUDA 通过平铺和共享内存提高性能
- 如何更改在 c++ 中使用提升库创建的共享内存的路径