-1

How do you make function foo() run for only a period of time? I think threads is the most common way of impementation.

How do I use bind and timed_join ??

george mano
  • 5,948
  • 6
  • 33
  • 43
  • 1
    What is `foo` doing that that would even be desirable? – ildjarn Jun 29 '12 at 00:12
  • I think this answers your question: http://stackoverflow.com/questions/1015494/can-i-create-a-software-watchdog-timer-thread-in-c-using-boost-signals2-and-th – loki11 Jun 29 '12 at 00:17
  • You should probably stick to [one question](http://stackoverflow.com/questions/11174598/stop-functions-in-5-minutes-if-they-dont-end-running). If you feel it needs more attention, you can put a bounty on it. – chris Jun 29 '12 at 01:21

3 Answers3

3

Code is not at war with other code. Code cooperates to get the job done. If you only want foo to run for a minute, code it to only run for a minute. Don't code it some other way and then try to force it to work some way other than the way it was coded.

If you want to use threads, you can code foo to check a synchronized "abort" flag. If foo notices the flag is set, it aborts. Then, after a minute, set that flag from another thread. Then foo will notice the flag is set and abort itself.

If you need to abort things like blocking I/O operations, the solutions are platform-specific. Or you can use a portable library that provides such capabilities, such as Boost.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    To put this in perspective, some threading APIs have "experimented" with commands a control thread can use to force cancellation of another thread - it's superficially attractive but in practice the cancelled thread can easily end up having some resources (heap, locks, file descriptors) that will then never be freed/unlocked/closed etc.. Many system and library calls take out such resources momentarily, so it's not just a matter of scanning your own source code. It's only really safe for very limited scenarios like intensive mathematical calculations. Few APIs have cancellation now. – Tony Delroy Jun 29 '12 at 00:42
2

I'm not exactly sure what your are asking. If you just need a function to do something for a minute, can you just wrap it in a while loop?

void foo(void)
{
    time_t end = time(NULL) + 60;
    while (time(NULL) <= end)
    {
        … // do something
    }
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • 1
    If the //do something statement runs for 20 minutes your code will run for 20 minutes long. I need my code to be checked every second so when the 60 seconds have passed it breaks out of the statement. Do you understand? – george mano Jun 29 '12 at 00:19
  • @georgemano : Then put the time check inside of the `//do something` as well. Your question is way too vague to be answered concretely. – ildjarn Jun 29 '12 at 00:21
  • 1
    @georgemano I don't know what to say, if you are writing code which never loops and runs for 20 minutes, then you have a much bigger set of problems than I've ever had to deal with. All the long running code I've seen has been intrinsically looped (networking loops, operations loops, animations loops, command loops, …). Sorry my advice wasn't of much help. – Jeffery Thomas Jun 29 '12 at 00:30
0

How do you make function foo() run for only a period of time?

The POSIX pthread APIs support cancellation, but immediate cancellation is not guaranteed by the standard. But below is an illustration of how it might be used to cancel a function foo after 1 second when it would normally run for 5 seconds.

void foo ()
{
    sleep(5);
}

void * start_foo (void *)
{
    int x;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &x);
    foo();
    return 0;
}

    pthread_t t;
    pthread_create(&t, 0, start_foo, 0);
    sleep(1);
    pthread_cancel(t);
    pthread_join(t, 0);

The start_foo function enables the cancellation state. The x parameter receives the old cancellation state.

How do I use bind and timed_join ??

I believe you are referring to boost::bind and boost::thread::timed_join. boost::bind provides a mechanism to define a functor that doesn't take any arguments, but calls a function that is passed an argument. boost::thread requires a functor that doesn't take an argument, so that is why it is used there.

boost::thread t(boost::bind(puts, "hello, world"));

boost::thread::timed_join is a join on the boost::thread that may return false if the thread has not finished within the time parameter passed to it.

boost::thread t(foo);
t.timed_join(boost::posix_time::seconds(1));
jxh
  • 69,070
  • 8
  • 110
  • 193