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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:22:45+00:00 2026-05-29T15:22:45+00:00

For a computation intensive problem I want to limit the CPU time spent by

  • 0

For a computation intensive problem I want to limit the CPU time spent by the program: if the program doesn’t find a solution within a given amount of time, I want the program to be terminated. Rather than having the program look for a solution forever, it should terminate if nothing is found. In case the platform matter, this is for UNIX. How can this be achieved?

  • 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-29T15:22:47+00:00Added an answer on May 29, 2026 at 3:22 pm

    Another POSIX solution that’s single-threaded and self-contained is to use signals:

    #include <unistd.h>
    #include <csignal>
    
    std::sig_atomic_t volatile done = 0;
    
    void game_over(int) { done = 1; }
    
    int main()
    {
        std::signal(SIGALRM, game_over);
        alarm(5); // this program will self-destruct in 5 seconds
    
        while (!done)
        {
            do_my_thing();  // or whatever; make sure this returns frequently
        }
    }
    

    (This is one of the very few legitimate and crucial uses of volatile: We must prevent the compiler from optimizing out the while (!done) conditional, and the compiler doesn’t see that done can be mutated, because it’s never touched inside the loop body.)

    POSIX discourages the use of std::signal in favour of its own, more powerful sigaction. Consult the manual if you’re interested, but for the simple purpose of raising an alarm this solution appears to suffice.

    If your program offers no interruption points at all (i.e. points at which you can check done), then you could also call abort() in the signal handler.

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

Sidebar

Related Questions

I wrote a multi-threaded program which does some CPU heavy computation with a lot
My current program is creating child processes and giving them work (CPU intensive work).
I am writing a small computation program with lot of read operations on blob
I am doing some computation on the CPU and then I transfer the numbers
I have an application written in native C++ which intends intensive computation. In fact
Is there any way to dynamically and seamlessly migrate a program / computation from
i have a highly data computation intensive application that has to process data received
How well does rails handle concurrent request when the controller is executing computation intensive
Sometimes, right after I submit a computation-intensive run to R in ESS, my whole
I have a computation task which is split in several individual program executions, with

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.