One of the tricky things with race conditions is that your code can seem to consistently work on some occasions or machines but not on other occasions or machines. This is because, if the thread you intended to go first just-so-happens to always win a race condition when you test it (for whatever reason), then it may appear that that thread is guaranteed to always go first and that there is no race condition. But, we can try to force a thread–perhaps one that we may not have expected to go first–to try to go first by using a sleep() call in another thread.
If you think that you have fixed the original pthreads code, try your solution out on the code directly below. This may reveal a problem with your solution. I have introduced a sleep() to help the consumer thread to win a race condition that may still exist in your code.