Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:22:13+00:00 2026-05-31T07:22:13+00:00

Is it efficient to SpinWait.SpinUntil(() => myPredicate(), 10000) for a timeout of 10000ms or

  • 0

Is it efficient to

SpinWait.SpinUntil(() => myPredicate(), 10000)

for a timeout of 10000ms

or

Is it more efficient to use Thread.Sleep polling for the same condition
For example something along the lines of the following SleepWait function:

public bool SleepWait(int timeOut)
{
    Stopwatch stopwatch = new Stopwatch(); 
    stopwatch.Start();
    while (!myPredicate() && stopwatch.ElapsedMilliseconds < timeOut)
    {
       Thread.Sleep(50)
    }  
    return myPredicate()
}

I’m concerned that all the yielding of SpinWait may not be a good usage pattern if we are talking about timeouts over 1sec? Is this a valid assumption?

Which approach do you prefer and why? Is there another even better approach?


Update – Becoming more specific:

Is there a way to Make BlockingCollection Pulse a sleeping thread when it reaches bounded capacity? I rather avoid a busy waits alltogether as Marc Gravel suggests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:22:14+00:00Added an answer on May 31, 2026 at 7:22 am

    The best approach is to have some mechanism to actively detect the thing becoming true (rather than passively polling for it having become true); this could be any kind of wait-handle, or maybe a Task with Wait, or maybe an event that you can subscribe to to unstick yourself. Of course, if you do that kind of “wait until something happens”, that is still not as efficient as simply having the next bit of work done as a callback, meaning: you don’t need to use a thread to wait. Task has ContinueWith for this, or you can just do the work in an event when it gets fired. The event is probably the simplest approach, depending on the context. Task, however, already provides most-everything you are talking about here, including both “wait with timeout” and “callback” mechanisms.

    And yes, spinning for 10 seconds is not great. If you want to use something like your current code, and if you have reason to expect a short delay, but need to allow for a longer one – maybe SpinWait for (say) 20ms, and use Sleep for the rest?


    Re the comment; here’s how I’d hook an “is it full” mechanism:

    private readonly object syncLock = new object();
    public bool WaitUntilFull(int timeout) {
        if(CollectionIsFull) return true; // I'm assuming we can call this safely
        lock(syncLock) {
            if(CollectionIsFull) return true;
            return Monitor.Wait(syncLock, timeout);
        }
    }
    

    with, in the “put back into the collection” code:

    if(CollectionIsFull) {
        lock(syncLock) {
            if(CollectionIsFull) { // double-check with the lock
                Monitor.PulseAll(syncLock);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How efficient is it to use sockets when doing IPC as compared to named
Which is more efficient for the compiler and the best practice for checking whether
Is there a more efficient way of doing the following? select * from foo
I've searching a efficient and elegant way to do this. I'll hope the example
How efficient is this query. It's selecting from the same table twice from within
Which one is more efficient over a very large set of files and should
How efficient is DNS second-level domain lookup? For example, in a url web.example.com, the
Why is it more efficient for me to hold an http connection open until
What's more efficient? Throwing an exception or throwing a fault... the way I see
Which way is more efficient? Is there a difference? This one: var str =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.