Skip to content

ThreadLocker

Gratt-5r2 edited this page Apr 9, 2022 · 1 revision

class ThreadLocker

void Enter() - enter to the critical section.
void Leave() - leave from the critical section.\ bool IsLocked() - checks the critical section is locked.

Example

ThreadLocker lck;
int TotalTicksCount = 0;

void foo( int ticks ) {
  for( int i = 0; i < ticks; i++ ) {
    lck.Enter();
    TotalTicksCount++;
    lck.Leave();
  }
}

void Test() {
  int ticksPerThread = 300000;
  Array<Thread> threads;
  for( int i = 0; i < 16; i++ )
    threads.Create( &foo ).Detach( (void*&)ticksPerThread );

  Thread::WaitForEnd( threads );
  Message::Info( "TotalTicksCount  == 300000 * 16 -> " + string( TotalTicksCount == 300000 * 16 ) );
}
Clone this wiki locally