0

I have implemented a barrier class MyBarrier which has single function wait() on which a calling thread must wait until all threads of his team have arrived. I want to find a good way to test my thread barrier class. Until now I have taken the following approach:

To test the thread barrier, I first create my barrier object

typedef unsigned int UINT;
UINT THREADS = 16;
MyBarrier barrier(THREADS);

Then I spawn a bunch of threads:

atomic<UINT> Counter = 0;
SpawnThreads( &MyBarrTestFunc, THREADS, barrier, Counter );
JoinThreads( THREADS );

The barrier test function which is spawned as a thread is as follows:

MyBarrTestFunc( UINT THREADS, MyBarrier& barrier, std::atomic<UINT> Counter )
{
    Counter.fetch_and_add(1);
    barrier.wait();
    assert((Counter% THREADS) == 0 ); 

    Counter.fetch_and_add(1);
    barrier.wait();
    assert((Counter % THREADS) == 0 );

    Counter.fetch_and_add(1);
    barrier.wait();
    assert((Counter% THREADS) == 0 );

    Counter.fetch_and_add(1);
    barrier.wait();
    assert((Counter % THREADS) == 0 );
}

I also created another Test Function to test my barrier inside a loop

const UINT HURDLES = 64;
MyBarrTestFunc( UINT THREADS, MyBarrier& barrier, std::atomic<UINT> Counter )
{ 
   for( UINT idx = 0; idx != HURDLES ; idx++ )
   {
        Counter.fetch_and_add(1);
        barrier.wait();
        assert((Counter % THREADS) == 0 );
   }
}

Can anyone suggest me if these are good ways to test a thread barrier ? If not, can you suggest a fool-proof way to test my barrier class ?

NOTE: My barrier class has in-built reset. Whenever a all threads of a team have reached the barrier(call to wait), one of the threads then resets the barrier to old state by so that it is ready to be reused by next team of threads

nurabha
  • 1,152
  • 3
  • 18
  • 42
  • Why is this question tagged `Java`? Java already has functionality similar to this in the `java.util.concurrent` package, but the question appears to be entirely about c++. – David Conrad Feb 26 '14 at 20:11
  • this question is abouttrying to implement implementing a thread library like java.util.concurrent.barriers. This problem is language oblivious! – nurabha Feb 26 '14 at 20:17

0 Answers0