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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:12:15+00:00 2026-06-03T14:12:15+00:00

Consider the following sample program in which a loop is running; int main() {

  • 0

Consider the following sample program in which a loop is running;

int main()
{

  for (int i = 0; i<= 300; ++i) {

  }

}

Pretty basic, now let’s suppose I want to print out the value of i every second:

cout << "i= " << i << "\n";

A simple loop like the following might suffice, where “elaspedTime” is a ficticious integer containing the number of seconds the program has been running magically updated by the OS:

int lastTime = 0;
while (true) {
  if (elapsedTime > lastTime) { // Another second has passed
    cout << "i= " << "\n";
    lastTime = elapsedTime;
  }
}

The ultimate goal here is to give an output like the following (assuming the loop ran exactly 100 times per second because it was on an old, slow CPU):

$ ./myprog
i= 100
i= 200
i= 300

These are simple functions and routines, despite this, I see no way to perform such an operation in a “classical” c++ program which typically has just a main() function. Despite the simplicity is this the point at which I need to learn multi-threading? Or, is it possible to call functions from main() and not wait for their return but without that called function “hogging” the 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-03T14:12:17+00:00Added an answer on June 3, 2026 at 2:12 pm

    What’s wrong with doing this? No multitasking required.

    int lastTime = 0;
    while (true) {
      if (std::time() > lastTime) { // Another second has passed
        cout << "i= " << i << "\n";
        // potentially expensive code goes here, which updates "i"
        lastTime = std::time();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following program: static void Main (string[] args) { int i; uint ui; i
Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
Consider the following code sample: interface IData { int Count(); } interface IOperations {
Consider a simple program in C++: #include <iostream> class link {}; int main() {
Consider the following code: [<EntryPoint>] let main (args: string []) = let rec main
Consider the following code sample: int i1 = 'w\'; int i2 = '\w\'; int
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Please consider following sample query SELECT * FROM subscriber WHERE subscription_date>DATE(NOW()-INTERVAL 10 DAY) My
Consider the following simple C program that read a file into a buffer and

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.