如果限制一个程序同时只能启动一个实例,有几个可以使用的库

main.cpp

限制程序同时只能启动一个实例只需要在 main() 函数里调用 if (!guard.tryToRun()) { return 0; } 即可。

1
2
3
4
5
6
7
8
9
10
11
12
#include "RunGuard.h"
 
int main(int argc, char *argv[]) {
RunGuard guard("9F0FFF1A-77A0-4EF0-87F4-5494CA8181C7");
 
if (!guard.tryToRun()) {
return 0;
}
 
QApplication a(argc, argv);
a.exec();
}

RunGuard.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef RUNGUARD_H
#define RUNGUARD_H
 
#include <QObject>
#include <QSharedMemory>
#include <QSystemSemaphore>
 
class RunGuard {
public:
RunGuard(const QString& key);
~RunGuard();
 
bool isAnotherRunning();
bool tryToRun();
void release();
 
private:
const QString key;
const QString memLockKey;
const QString sharedmemKey;
 
QSharedMemory sharedMem;
QSystemSemaphore memLock;
 
Q_DISABLE_COPY(RunGuard)
};
#endif // RUNGUARD_H

RunGuard.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "RunGuard.h"
#include <QCryptographicHash>
 
namespace {
 
QString generateKeyHash(const QString& key, const QString& salt) {
QByteArray data;
 
data.append(key.toUtf8());
data.append(salt.toUtf8());
data = QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex();
 
return data;
}
 
}
 
 
RunGuard::RunGuard(const QString &key)
: key(key)
, memLockKey(generateKeyHash(key, "_memLockKey"))
, sharedmemKey(generateKeyHash(key, "_sharedmemKey"))
, sharedMem(sharedmemKey)
, memLock(memLockKey, 1) {
memLock.acquire();
{
QSharedMemory fix(sharedmemKey); // Fix for *nix: http://habrahabr.ru/post/173281/
fix.attach();
}
memLock.release();
}
 
RunGuard::~RunGuard() {
release();
}
 
bool RunGuard::isAnotherRunning() {
if (sharedMem.isAttached()) {
return false;
}
 
memLock.acquire();
const bool isRunning = sharedMem.attach();
if (isRunning) {
sharedMem.detach();
}
memLock.release();
 
return isRunning;
}
 
bool RunGuard::tryToRun() {
if (isAnotherRunning()) { // Extra check
return false;
}
 
memLock.acquire();
const bool result = sharedMem.create(sizeof(quint64));
memLock.release();
 
if (!result) {
release();
return false;
}
 
return true;
}
 
void RunGuard::release() {
memLock.acquire();
 
if (sharedMem.isAttached()) {
sharedMem.detach();
}
 
memLock.release();
}
 

http://www.qtdebug.com/qtbook-misc-single-application/

SingleApplication
https://github.com/itay-grudev/SingleApplication

Socket也有专门的Unicode版本的更多相关文章

  1. VC++关于UNICODE版本的开发

    关于UNICODE版本的开发 代码转换方案 概述 在VC6.0中,相应的有一些宏来代替ANSI的函数.宏或数据类型,这些宏在ANSI编译条件中处理字符串为单字节,而在UNICODE中处理字符串为双字节 ...

  2. Mono 3.2.3 Socket功能迎来一稳定的版本

    由于兴趣自己业余时间一直在搞.net下面的通讯应用,mono的存在得以让.NET程序轻松运行在Linux之下.不过经过多尝试Socket相关功能在Mono下的表现并不理想.不管性能还是吞吐能力方面离我 ...

  3. c++ Socket客户端和服务端示例版本三(多线程版本)

    客户端 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/soc ...

  4. c++ Socket客户端和服务端示例版本二

    客户端 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/soc ...

  5. c++ Socket客户端和服务端示例版本一

    客户端 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/soc ...

  6. 使用socket.io client 开发时兼容IE低版本的办法

    使用socket.io client 开发时兼容IE低版本的办法 socket.io提供了针对各个版本浏览器的‘socket’功能的封转:websocket,长连接,流,flash什么的.给你格式化下 ...

  7. 高性能 Socket 组件 HP-Socket v3.2.1 正式发布

    HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C ...

  8. 「Python」socket指南

    开始 网络中的 Socket 和 Socket API 是用来跨网络的消息传送的,它提供了 进程间通信(IPC) 的一种形式.网络可以是逻辑的.本地的电脑网络,或者是可以物理连接到外网的网络,并且可以 ...

  9. 玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理

    Windows服务Debug版本 注册 Services.exe -regserver 卸载 Services.exe -unregserver Windows服务Release版本 注册 Servi ...

随机推荐

  1. hdu 1255 覆盖的面积

    http://acm.hdu.edu.cn/showproblem.php?pid=1255 #include <cstdio> #include <cstring> #inc ...

  2. LeetCode_Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 很挫的一个想 ...

  3. 【转】Linux下Android ADB驱动安装详解

    原文网址:http://blog.csdn.net/zhenwenxian/article/details/5901350 Linux下Android ADB驱动安装详解 概述 最近由于内置的合作商比 ...

  4. Best Meeting Point 解答

    Question A group of two or more people wants to meet and minimize the total travel distance. You are ...

  5. UVa232.Crossword Answers

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. OS error set

    Failed to resolve/decode supposed IPv4 source addres Failed to resolve/decode supposed IPv4 source a ...

  7. 【转】Linux awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  8. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  9. 把分类的select写在moden里做成一个组件 高洛峰

    function selectform($name="pid", $pid=0) { $data = $this->field('id, concat(path, " ...

  10. 【多线程】--生产者消费者模式--Lock版本

    在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...