Interview Question

Senior Software Development Engineer In Test (SDET) Interview Redmond, WA

Implement a counting semaphore using only binary mutexes

Tags:
programming, c++
Answer

Interview Answer

1 Answer

1

int counter = 0;
mutex m;

sempahore_increase()
{
lock(m);
counter++;
unlock(m);
}

semaphore_decrease()
{
while (true)
{
lock(m);
if (counter)
{
counter--;
unlock(m);
sleep(1000);
return;
}
unlock(m);
}
}

Azh on Sep 9, 2010

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up