locked
Is there a way to release a threads time slice

    Question

  • Hi all,

    I have a situation where I want to replace calls to Sleep(0) in some existing code.

    The Sleep was used only to release the current threads time slice. It's not there to add a delay to a thread.

    I know C# has the Thread.Yield() method which is kind of what I'm looking for.

    However I have implemented my threading model using the ThreadPool system. So is there a way of yielding the thread. Our threading system has low contention but there are cases when we do have to wait a brief amount of time before accessing a resource. I could use a critical section with a spin count on it. I guess that internally will yield the thread after a certain number of spins but that is implementation specific.

    As we have low contention I don't want to add heavy synchronisation objects to protect the resource. This would just cause a loss of performance. I would rather have the rare case where contention is detected to yield the current thread and try again on the next time slice.

    Is this possible in Windows App Store programs, or should I just use a CS with a spin count on it.

    Thanks



    • Edited by Mortom Tuesday, February 26, 2013 1:01 PM
    Tuesday, February 26, 2013 1:00 PM

All replies

  • Is YieldProcessor what you are looking for? Take a look at the list of allowed Win32 Threads and synchronization API

    --Rob

    Wednesday, February 27, 2013 2:04 AM
    Owner
  • Definitely use a CS with SpinCount. It'll give you great performance and you won't be recreating the wheel with your own check+Sleep(0) implementation (which will probably be buggy).

    Wednesday, February 27, 2013 6:45 PM