Your while strategy is sound in concept but you can't count on decrement to do the right thing. Before the loop begins, calculate the deadline after which your loop should end, and then test within the loop to see if that deadline has now been passed. But take care that you are not "busy waiting."