/// Find all errors
#include <windows.h>
#include <process.h>
/// Critical section guard
/// Closes critical section at the end of the visibility scope
class CSGuard
{
private:
/// Controled critical section
CRITICAL_SECTION* cs_;
public:
/// Constructor
/// Locks critical section
CSGuard( CRITICAL_SECTION* cs )
: cs_( cs )
{
EnterCriticalSection( cs_ );
}
/// Destructor
/// Unlocks critical section
~CSGuard()
{
LeaveCriticalSection( cs_ );
}
};
/// Performs some background work
class SomeClass
{
private:
/// Thread descriptor
HANDLE thread_;
/// Stop falg
volatile bool exit_;
/// Critical section
CRITICAL_SECTION cs_;
public:
/// Constructor
SomeClass()
: thread_( reinterpret_cast<HANDLE>( _beginthread( &thread_func, 0, this ) ) )
{
InitializeCriticalSection( &cs_ );
}
/// Destructor
~SomeClass()
{
stop();
DeleteCriticalSection( &cs_ );
}
private:
/// Stops background task and wait until task is terminated
void stop()
{
CSGuard g( &cs_ );
exit_ = true;
WaitForSingleObject( thread_, INFINITE );
}
/// Background task
static void thread_func( void* args )
{
SomeClass* p_this = reinterpret_cast< SomeClass* >( args );
for( ;; ) // infinite loop
{
{
CSGuard g( &p_this->cs_ );
if( p_this->exit_ )
break;
}
Sleep( 100 ); // do some work
}
}
};
/// Test
int main()
{
SomeClass sc;
return 0;
}
Replies to Find all errors
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}