top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is an easy way to implement a time-out on a function so it is forced to terminate after a certain time has passed?

+2 votes
411 views

I know of "select()" which works using file descriptors.

However I want similar functionality to "select()" but for a simple function that returns an int.

So suppose, there's "int calculate_magic()" function and I want to allow it to work for 2 seconds, if it takes longer than that I want to move on.

If "calculate_magic()" wrote its answer to a file descriptor I could use "select()" but how to do it for the case I mentioned?

I need something like this for a simple game implementation where the player is allowed a maximum time to make a decision about their next move.
I
was hoping I can get something without having to deal with threads, etc.

posted Jul 29, 2014 by Pardeep Kohli

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+6 votes

What is the simples way to check if the sum of two unsigned integers has resulted in an overflow.

+6 votes

For example: It returns ‘b’ when the input is “abaccdeff”.

+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

...