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++