Is it safe to use Sleep() function on Windows in game loop (C++)? I want to have fixed frame rate.
No, it's not. Sleep only guarantees a minimum time to sleep for, but it may actually sleep for any arbitrary amount of time over that. Your timer resolution (set via timeBeginPeriod) is also important for it, and even if you're using something else (like QueryPerformanceCounter) for your timer, you still need timeBeginPeriod to control Sleep.
So in summary:
- Time to sleep for is only a guaranteed minimum and actual sleep time may be higher.
- Sensitive to value set (or not set) via timeBeginPeriod.
The only reasonable case for using Sleep would be if you wanted to reduce CPU usage for e.g. mobile devices. Even then you'd be using Sleep (1); using it to control framerates is not the way to go.
Forcing vsync on is probably one common way to get a fixed framerate, but even then, different hardware will run at different refresh rates and you won't be able to have a consistent fixed framerate across different machines (this depends on what kind of hardware you're targetting of course).
At this point in time it's necessary to mention this article: Gaffer On Games - Fix Your Timestep!. That describes the steps you need to get a consistent timestep in your simulation.
Short answer: no, it is not.
To have a fixed frame rate you have to call a certain callback function that forces a specific frame rate. Obviously, if you don't now how long a single iteration in a loop will take you cannot set a fix sleep time.