Une
section critique est un bout de code qui ne peut être interrompu pas d'autres threads d'un même processus pendant son exécution. C'est un bloc d'instructions qui doit être exécuté de manière atomique dans un processus donné. Il doit donc être court (en temps d'exécution) et fini (ce n'est pas un bon endroit pour se lancer dans une série d'entrées/sorties).
// main.cpp (Programme principal)...
#include "cCriticalSectionInit.h"
#include "cCriticalSectionLock.h"
#include <iostream>
int main(int argc, char **argv)
{
cCriticalSectionInit oCriticalSectionInit;
std::cout << "cCriticalSectionLock::isLocked(eCS_WDBG_SYM)...: "
<< cCriticalSectionLock::isLocked(eCS_WDBG_SYM) << std::endl;
cCriticalSectionLock oCriticalSectionLock(eCS_WDBG_SYM);
std::cout << "cCriticalSectionLock::isLocked(eCS_WDBG_SYM)...: "
<< oCriticalSectionLock.isLocked() << std::endl;
std::cout << "cCriticalSectionLock::isLocked(eCS_WDIR_WATCH)...: "
<< cCriticalSectionLock::isLocked(eCS_WDIR_WATCH) << std::endl;
cCriticalSectionLock oCriticalSectionLock2(eCS_WDIR_WATCH);
std::cout << "cCriticalSectionLock::isLocked(eCS_WDIR_WATCH)...: "
<< oCriticalSectionLock2.isLocked() << std::endl;
return 0;
}
...
// cCriticalSectionList.h (Liste des sections critiques)...
#pragma once
enum eCriticalSection
{
eCS_WDBG_SYM,
eCS_WDIR_WATCH
};
class cCriticalSectionList
{
friend class cCriticalSectionInit;
friend class cCriticalSectionLock;
private:
cCriticalSectionList();
public:
~cCriticalSectionList();
void initAll() const;
void deleteAll() const;
void lock(eCriticalSection _id) const;
void unlock(eCriticalSection _id) const;
bool isLocked(eCriticalSection _id) const;
};
...
// cCriticalSectionList.cpp (Liste des sections critiques)...
#include "cCriticalSectionList.h"
#include <windows.h>
#include <iostream>
static const int gCS_NUM = 2;
static CRITICAL_SECTION gCriticalSection[gCS_NUM];
cCriticalSectionList::cCriticalSectionList()
{
}
cCriticalSectionList::~cCriticalSectionList()
{
}
void cCriticalSectionList::initAll() const
{
for (int i = 0; i < gCS_NUM; i++)
{
InitializeCriticalSection(&gCriticalSection[i]);
}
std::cout << "cCriticalSectionList::initAll()..." << std::endl;
}
void cCriticalSectionList::deleteAll() const
{
for (int i = 0; i < gCS_NUM; i++)
{
DeleteCriticalSection(&gCriticalSection[i]);
}
memset(gCriticalSection, 0, sizeof(gCriticalSection));
std::cout << "cCriticalSectionList::deleteAll()..." << std::endl;
}
void cCriticalSectionList::lock(eCriticalSection _id) const
{
EnterCriticalSection(&gCriticalSection[_id]);
std::cout << "cCriticalSectionList::lock()...: " << _id << std::endl;
}
void cCriticalSectionList::unlock(eCriticalSection _id) const
{
LeaveCriticalSection(&gCriticalSection[_id]);
std::cout << "cCriticalSectionList::unlock()...: " << _id << std::endl;
}
bool cCriticalSectionList::isLocked(eCriticalSection _id) const
{
const BOOL isTryEnterCriticalSection = TryEnterCriticalSection(&gCriticalSection[_id]);
if (!isTryEnterCriticalSection)
return true;
LeaveCriticalSection(&gCriticalSection[_id]);
return false;
}
...
// cCriticalSectionInit.h (Initialisation d'une section critique)...
#pragma once
class cCriticalSectionInit
{
public:
cCriticalSectionInit();
~cCriticalSectionInit();
};
...
// cCriticalSectionInit.cpp (Initialisation d'une section critique)...
#include "cCriticalSectionInit.h"
#include "cCriticalSectionList.h"
cCriticalSectionInit::cCriticalSectionInit()
{
cCriticalSectionList oCriticalSectionList;
oCriticalSectionList.initAll();
}
cCriticalSectionInit::~cCriticalSectionInit()
{
cCriticalSectionList oCriticalSectionList;
oCriticalSectionList.deleteAll();
}
...
// cCriticalSectionLock.h (Verrouillage d'une section critique)...
#pragma once
#include "cCriticalSectionList.h"
class cCriticalSectionLock
{
public:
cCriticalSectionLock(eCriticalSection _id);
~cCriticalSectionLock();
bool isLocked() const;
static bool isLocked(eCriticalSection _id);
private:
eCriticalSection m_id;
};
...
// cCriticalSectionLock.cpp (Verrouillage d'une section critique)...
#include "cCriticalSectionLock.h"
cCriticalSectionLock::cCriticalSectionLock(eCriticalSection _id)
: m_id(_id)
{
cCriticalSectionList oCriticalSectionList;
oCriticalSectionList.lock(m_id);
}
cCriticalSectionLock::~cCriticalSectionLock()
{
cCriticalSectionList oCriticalSectionList;
oCriticalSectionList.unlock(m_id);
}
bool cCriticalSectionLock::isLocked() const
{
cCriticalSectionList oCriticalSectionList;
return oCriticalSectionList.isLocked(m_id);
}
bool cCriticalSectionLock::isLocked(eCriticalSection _id)
{
cCriticalSectionList oCriticalSectionList;
return oCriticalSectionList.isLocked(_id);
}
...
// Terminal (Exécuter le projet)...
rdvcpp.exe
...