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

  • SEARCH
  • Home
  • 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 8669711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:32:35+00:00 2026-06-12T18:32:35+00:00

I am writing a DCPU-16 emulator and I am calculating the real time clock

  • 0

I am writing a DCPU-16 emulator and I am calculating the real time clock speed of the CPU by launching a thread that calls the function getRealTimeCPUClock() in a separate thread. The problem is it seems that the future object’s “valid” attribute is true even when it has not returned a value. As a result, when calling futureObj.get(), it then waits for getRealTimeCPUClock() to return.

With a launch policy of async (as opposed to deferred) isn’t it supposed to launch the function into the background and then when it returns set the valid attribute to true?

Is this the wrong usage?

int getRealTimeCPUClock() {
    int cyclesBeforeTimer = totalCycles;
    sleep(1);

    return totalCycles - cyclesBeforeTimer;
}

void startExecutionOfProgram(char *programFileName)
{
    size_t lengthOfProgramInWords = loadProgramIntoRAM(programFileName);
    auto futureRealTimeClockSpeed = std::async(std::launch::async, getRealTimeCPUClock);

    while(programCounter < lengthOfProgramInWords) {

        if(futureRealTimeClockSpeed.valid()) {
            realTimeClockSpeed = futureRealTimeClockSpeed.get();
            futureRealTimeClockSpeed = std::async(std::launch::async, getRealTimeCPUClock);
        }
        step(); 
    }
}
  • 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-12T18:32:36+00:00Added an answer on June 12, 2026 at 6:32 pm

    valid() does not what you think it does (although the entry in cppreference suggests otherwise).

    Here is what the Standard says about valid():

    (§ 30.6.6/18)
    bool valid() const noexcept;

    Returns: true only if *this refers to a shared state.

    The value returned by valid() will be true as long as long as the future object is associated with a valid shared state, which is generally the case after you launched it using std::async and before you retrieve the result (using get()). The future will also be invalidated when you use the share() method to create a shared_future. None of this is related to what you are trying to do, i.e. checking whether the result is available.

    To determine whether the result of a future is ready, I suggest using the wait_for() function with a delay of 0:

    if (futureRealTimeClockSpeed.wait_for(std::chrono::seconds(0))
              == std::future_status::ready)
       /*...*/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a recursive function, I want one fn that executes when the list has
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing documentation in html requires some code examples. What to do with characters that
Writing a client application that sends images to a server via a webservice. As
Writing a sound control applet with GUI that communicates with a device via USB.
Writing an app that will include the ability to decompress zip and rar files.
Writing another question for SO, I came to a pattern that I use very
writing a recursive string reverse function out of curiosity, but having a bit of
Writing a SQL query that should return the average charges for a preceding 91
Writing a function to generate and push some random data inside of the unknown

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.