qt怎么设置一个对象只实例化一次
展开全部
1. 共享内存的方法
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.
(据说这种方法在Linux系统下会有一个内存释放的问题,在某种情况下会引起程序的异常或崩溃)
[cpp] view plain copy
// 确保只运行一次
QSystemSemaphore sema("JAMKey",1,QSystemSemaphore::Open);
sema.acquire();// 在临界区操作共享内存 SharedMemory
QSharedMemory mem("SystemObject");// 全局对象名
if (!mem.create(1))// 如果全局对象以存在则退出
{
QMessageBox::information(0, MESSAGEB
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.
(据说这种方法在Linux系统下会有一个内存释放的问题,在某种情况下会引起程序的异常或崩溃)
[cpp] view plain copy
// 确保只运行一次
QSystemSemaphore sema("JAMKey",1,QSystemSemaphore::Open);
sema.acquire();// 在临界区操作共享内存 SharedMemory
QSharedMemory mem("SystemObject");// 全局对象名
if (!mem.create(1))// 如果全局对象以存在则退出
{
QMessageBox::information(0, MESSAGEB
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询