'How to reset a POSIX semaphore?

Maybe I am wrong but it seems to me that the semaphore from this link is missing a reset function.

Is there another way to reset its counter? I found this implementation but does it really reset the semaphore?

void Semaphore::Reset()
{
    while(sem_trywait(&sem) == 0)
    {
    }
}

Thanks!



Solution 1:[1]

We keep reset the value of the semaphore to the initial value.

sem_getvalue(&sem, &value );
while(value > INITIAL_VAL  )
{
    sem_trywait(&sem);
    sem_getvalue(&sem, &value );
}

Disclamier: Untested code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1