tests/semaphore_unit_test.cc
TEST_F(SemaphoreUnitTest, InitCountAsTenWithTwentyConsumers) {
  Semaphore semaphore(10);
  std::atomic<int> timeouts{0};
  auto const wait_duration = std::chrono::milliseconds(1000);
  std::vector<std::thread> threads;

  for (int i = 0; i < 20; i++) {
    threads.push_back(std::thread([&semaphore, &wait_duration, &timeouts] () {
      auto now = std::chrono::high_resolution_clock::now();
      auto wait_result = semaphore.WaitUntil(now + wait_duration);
      // Increment the timeouts counter if a consumer thread times out on wait
      if (!wait_result) {
        timeouts++;
      }
    }));
  }

  // Join the threads and clear the placeholder for threads
  for (auto& t : threads) {
    t.join();
  }
  threads.clear();

  EXPECT_EQ(timeouts.load(), 10);
}
// Initialize a semaphore with a count of 10, and have 20 consumer threads to
// wait on this semaphore. 10 of them returns immediately and the remaining
// 10 timeout
Initialize a semaphore with a count of 10, and have 20 consumer threads to
wait on this semaphore. 10 of them returns immediately and the remaining
10 timeout
Initialize a semaphore with a count of 10 and have 20 consumer threads to wait on this semaphore . 10 of them returns immediately and the remaining 10 timeout
TEST_F(SemaphoreUnitTest, InitCountAsTenWithTwentyConsumers) {
  Semaphore semaphore(10);
  std::atomic<int> timeouts{0};
  auto const wait_duration = std::chrono::milliseconds(1000);
  std::vector<std::thread> threads;
  for (int i = 0; i < 20; i++) {
    threads.push_back(std::thread([&semaphore, &wait_duration, &timeouts] () {
      auto now = std::chrono::high_resolution_clock::now();
      auto wait_result = semaphore.WaitUntil(now + wait_duration);
      if (!wait_result) {
        timeouts++;
      }
    }));
  }
  for (auto& t : threads) {
    t.join();
  }
  threads.clear();
  EXPECT_EQ(timeouts.load(), 10);
}
TEST_F ( SemaphoreUnitTest , InitCountAsTenWithTwentyConsumers ) { Semaphore semaphore ( 10 ) ; std :: atomic < int > timeouts { 0 } ; auto const wait_duration = std :: chrono :: milliseconds ( 1000 ) ; std :: vector < std :: thread > threads ; for ( int i = 0 ; i < 20 ; i ++ ) { threads . push_back ( std :: thread ( [ & semaphore , & wait_duration , & timeouts ] ( ) { auto now = std :: chrono :: high_resolution_clock :: now ( ) ; auto wait_result = semaphore . WaitUntil ( now + wait_duration ) ; if ( ! wait_result ) { timeouts ++ ; } } ) ) ; } for ( auto & t : threads ) { t . join ( ) ; } threads . clear ( ) ; EXPECT_EQ ( timeouts . load ( ) , 10 ) ; }
Initialize a semaphore with a count of 10, and have 20 consumer threads to
wait on this semaphore.
Initialize a semaphore with a count of 10 and have 20 consumer threads to wait on this semaphore .
// Increment the timeouts counter if a consumer thread times out on wait // Join the threads and clear the placeholder for threads