Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A program consists of n threads. Each thread works in phases. Each thread begins

ID: 663998 • Letter: A

Question

A program consists of n threads. Each thread works in phases. Each thread begins in phase 1. It
is required that all threads complete phase 1 before they can begin phase 2. Likewise, each thread must
complete phase 2 before any thread can begin phase 3, and so on. So, the code for each thread has the
following structure.
for (i = 1; ;i++)
{
Execute phase i
WaitForBarrier(i, threadid);
}
The function WaitForBarrier ensures that if a thread finishes phase i too quickly then it is blocked. When
all threads finish phase i, they are resumed so that they can continue executing phase i+1.
Write the function WaitForBarrier(int, int) using semaphores. Explain the correctness of your solution.

Please code in C++

Explanation / Answer

// Standard headers #include #include // External headers #include "gmock/gmock.h" // Internal headers #include "Barrier.hpp" static unsigned int kNumThreads = std::thread::hardware_concurrency()-1; static unsigned int kNumCycles = 100; static void WaitForBarrier(Barrier *barrier, unsigned int n_cycles) { for (int i = 0; i wait(); } class ABarrierRun : public testing::Test { }; TEST_F(ABarrierRun, waitForBarrierOnce) { Barrier barrier(kNumThreads); std::vector threads; for (unsigned int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote