" " , ; , , .
RAII . , RAII Sepration Of Concerns, :
- / CRITICAL_SECTION
- / CRITICAL_SECTION
CS, , SoC , . , , /. , psudocode:
void WorkerThreadProc(CCriticalSection cs)
{
cs.Enter();
cs.Leave();
}
int main()
{
CCriticalSection my_cs;
std::vector<NeatStuff> stuff_used_by_multiple_threads;
for( int i = 0; i < 3; ++i )
CreateThread(... &WorkerThreadProc, my_cs);
wait();
}
CCriticalSection , 4 . , , CRITICAL_SECTION . , .
, , . , , "" , ? shared_ptr, "" , , .
"" , . :
class CCriticalSection : public CRITICAL_SECTION
{
public:
CCriticalSection(){ InitializeCriticalSection(this); }
~CCriticalSection() { DestroyCriticalSection(this); }
};
... ...
class CSLock
{
public:
CSLock(CRITICAL_SECTION& cs) : cs_(cs) { EnterCriticalSection(&cs_); }
~CSLock() { LeaveCriticalSection(&cs_); }
private:
CRITICAL_SECTION& cs_;
};
CCriticalSection, , const, CSLocks. CSLock , , , CCriticalSection ; .