Can someone explain me this:
if inserting Thread.Yield() anywhere in your code makes or breaks the program, you almost certainly have a bug.
I’ve read it here : http://www.albahari.com/threading/
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I can only guess to the author’s intent, but: thread scheduling is already indeterminate. If adding the yield makes it work, then I infer that the reality here is that it is relying on the side-effect of a race condition, i.e. giving some CPU to another thread allows the timings to coincide such that the right thing happens. Well, you don’t know what you are yielding to on a parallel system, and on a milticore system you may well not be yielding to the same thing (yield is same CPU only). As such, the only sane way to do this is with a structure such as a Monitor, Mutex, Semaphore, ResetEvent, or some other locking primitive designed for allowing controlled flow between threads and (sometimes) processes.