如何通过 QsystemSemphore 强制我的应用程序成为单一进程? Qt、Linux
Possible Duplicate:
How to force my application to open one exe only? qt, linux
Hi,
I want to force my application to open one exe only, how to do it by QsystemSemaphore?
i.e. if the proce
10x!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用 QSystemSemaphore 不容易解决这个问题。据我所知,此类仅支持阻止锁定尝试。这似乎是这里的要点:你只能调用release(总是成功,但不会告诉你任何东西)或acquire(它只会告诉你你永远阻止你达到了极限):
如果一个实例创建了一个信号量,它不知道它是否真的创建了它或者是否使用了现有的。如果它是一个二进制信号量,则可能会发生两件事。它要么获得锁,这意味着它是第一个也是唯一的实例,要么它只是阻塞在那里,直到第一个实例退出。
为了绕过这个问题,你可以将该测试放入一个单独的线程中,这样你就可以在该线程上使用外部超时来检查它是否被阻止,但是诚实地尝试这样的做法是肮脏的且极其危险的,没有办法让它工作 100 % 这样也安全。
当您谈论 exe 时,可能会认为这可以仅在 Windows 平台上解决?
对于更便携的解决方案,可以使用锁定文件来完成非常类似的事情(打开一个临时文件以进行独占访问,我认为这就是 QtSingleApplication 所做的),但我个人不喜欢那种基于文件的解决方法。
I don't think thats easily solvable using a QSystemSemaphore. As far as I see, this class only supports blocking lock attempts. That seams to be the major point there: you can only call release(succeeds always but doesn't tell you anything) or acquire(which only tells you that you hit the limit by blocking you forever):
If a instance creates a semaphore it won't know if it really created it or if its using a existing one. If it's a binary semaphore at acquire two things might happen. Either it gets the lock, meaning its the first and only instance, or it just blocks there until the first instance quits.
To bypass that problem you might put that test into a separate thread, so you could check if it gets blocked using a external timeout at that thread, but honesty attempts like that are dirty and extreme risky, there is no way to get it working 100% safely that way too.
As you talk about a exe, might be assumed this could be solved windows platform-only?
For more portable solutions something very similar can be done using a lock-file(open a temporary file for exclusive access, I think thats what QtSingleApplication does), but I personally don't like that file-based workaround.