Let’s say I have one Mutex, two threads, one function and one cycle (Pseudo code).
Function:
void Update(){
Mutex.enter();
...// time: 10 ms
Mutex.leave();
}
Main.cpp:
void main(){
...// Starting thread
while(true)
Update();
}
Thread:
void Thread(void *){
Mutex.enter();
... //
Mutex.leave();
}
But Function calls constantly, so Mutex small time is free. How high chance Thread have to enter in Mutex? If low, how it can be resolved?
If you’re using boost threads (link), then I’d use
yield(). It’ll allow any other “waiting” threads to “get a chance” to run.There’s probably a win32 or pthreads way of doing this too.
Edit: and by the way, use
yield()outside of the locks. If it’s inside the locks, obviously that would be useless.Edit2: And here’s the functions for different platforms:
SwitchToThread()msdn link.If you’re not on any of those platforms, read the descriptions at those links, and look for a function that does the same thing in your framework.