Class to get a thread safe count of previous Instances | CodeGuru

Class to get a thread safe count of previous Instances

There have been many examples,posted on CodeGuru, of how to check for a previously running instance of an application. Some used DDE, others mutexs but what struck me as the easiest suggestion was to use a ‘shared section’ to store a flag – accessable by every instance. The following class uses that technique, along with […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 12, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

There have been many examples,posted on CodeGuru, of how to check for a previously running instance of
an application. Some used DDE, others mutexs but what struck me as the easiest suggestion was to use
a ‘shared section’ to store a flag – accessable by every instance.

The following class uses that technique, along with a couple of the ‘interlocked’ functions to make
it threadsafe. Instead of a simple flag, a count of running instances is kept. The count obtained by
the class is only a snapshot.

To use the class simply create a static instance of it. Then check the ‘Count()’ method to determine
how many instances of your application where running before the CPreviousInstance object was
instantiated.

Code tested on WinNT 4.0 spk 4 VC6.0 spk 2

// CPreviousInstance.h
class CPreviousInstance
{
public:
	CPreviousInstance();
	virtual ~CPreviousInstance();
	LONG Count() const
	{
		return m_previous;
	}
private:
	static LONG		s_count;
	LONG			m_previous;
	CPreviousInstance(const CPreviousInstance&);
	CPreviousInstance& operator=(const CPreviousInstance&);
};
// CPreviousInstance.cpp
// static instance count stored in a shared read/write section
#pragma data_seg(“Instance”)
LONG  CPreviousInstance::s_count = 0;
#pragma data_seg()
#pragma comment(linker,”/section:Instance,rws”)
// Construction/Destruction
CPreviousInstance::CPreviousInstance():
m_previous(0)
{
	m_previous = ::InterlockedIncrement(&s_count);
	–m_previous;
}
CPreviousInstance::~CPreviousInstance()
{
	::InterlockedDecrement(&s_count);
}

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.