找回密码
 立即注册
收起左侧

哪位给解释下面代码

1
回复
7335
查看
[复制链接]
累计签到:2 天
连续签到:1 天
来源: 2015-8-17 16:11:55 显示全部楼层 |阅读模式
1Qter豆

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSharedMemory *shareMem = new QSharedMemory(QString("SingleInstanceIdentify"));

    /* if the sharedmemory has not been created, it returns false, otherwise true.
     * But if the application exit unexpectedly, the sharedmemory will not detach.
     * So, we try twice.
     */
    volatile short i = 2;
    while (i--)
    {
        if (shareMem->attach(QSharedMemory::ReadOnly)) /* no need to lock, bcs it's read only */
        {
            shareMem->detach();
        }
    }

    if (shareMem->create(1))

    {
        MainWindow w;
        w.show();
        a.exec();
        
        if (shareMem->isAttached())
            shareMem->detach();
        delete shareMem;
    }

    return 0;
}


最佳答案

查看完整内容

你理解QSharedMemory是干嘛的就行了。 QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access. When using this class, be aware of the following platform differences: Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that hav ...
回复

使用道具 举报

尚未签到

2015-8-17 16:11:56 显示全部楼层
你理解QSharedMemory是干嘛的就行了。

QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.

When using this class, be aware of the following platform differences:

Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Windows kernel releases the shared memory segment automatically.
Unix: QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.
HP-UX: Only one attach to a shared memory segment is allowed per process. This means that QSharedMemory should not be used across multiple threads in the same process in HP-UX.
Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

Unlike QtSharedMemory, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了