Implement dynamic semaphores

This pull request implements *dynamic semaphores* mentioned in issues #36.