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 8142963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:58:11+00:00 2026-06-06T12:58:11+00:00

In a multi threaded app, is while (result->Status == Result::InProgress) Sleep(50); //process results better

  • 0

In a multi threaded app, is

while (result->Status == Result::InProgress) Sleep(50);
//process results

better than

while (result->Status == Result::InProgress);
//process results

?
By that, I’m asking will the first method be polite to other threads while waiting for results rather than spinning constantly? The operation I’m waiting for usually takes about 1-2 seconds and is on a different thread.

  • 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-06-06T12:58:13+00:00Added an answer on June 6, 2026 at 12:58 pm

    It’s better, but not by much.

    As long as result->Status is not volatile, the compiler is allowed to reduce

    while(result->Status == Result::InProgress);
    

    to

    if(result->Status == Result::InProgress) for(;;) ;
    

    as the condition does not change inside the loop.

    Calling the external (and hence implicitly volatile) function Sleep changes this, because this may modify the result structure, unless the compiler is aware that Sleep never modifies data. Thus, depending on the compiler, the second implementation is a lot less likely to go into an endless loop.

    There is also no guarantee that accesses to result->Status will be atomic. For specific memory layouts and processor architectures, reading and writing this variable may consist of multiple steps, which means that the scheduler may decide to step in in the middle.

    As all you are communicating at this point is a simple yes/no, and the receiving thread should also wait on a negative reply, the best way is to use the appropriate thread synchronisation primitive provided by your OS that achieves this effect. This has the advantage that your thread is woken up immediately when the condition changes, and that it uses no CPU in the meantime as the OS is aware what your thread is waiting for.

    On Windows, use CreateEvent and co. to communicate using an event object; on Unix, use a pthread_cond_t object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a small multi-threaded app that uses asynchronous TCP sockets, but I will
I have got a multi-threaded app that process a very large data file. Works
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
My multi-threaded app segfaults on a call to PyImport_ImportModule(my_module) . The BT will be
I have a multi threaded app that writes log to file. occasionaly, saving fails
I'm new to C++ and am writing a multi-threaded app whereby different writers will
I have a multi threaded JAVA app that uses a shared c lib. What
Because my app is multi-threaded I use two NSManagedObjectContexts. The main context, that runs
I have a multi-threaded Cocoa app that processes images. The program has a progress
If I write a multi-threaded java application, will the JVM take care of utilizing

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.