0

I have a situation as follows:

int funcA()
{
   /*There will call  funcB*/
   funcB();
}

funcB() maybe last for a long time. And if I find it has been running for over 5 minutes, I want to abort funcB() and continue to do other thing.

How can I do this?

  • 5
    You need to run `funcB` in a separate thread, run the timer in your current thread, and stop the long-running thread when the time is up. – Sergey Kalinichenko Jan 06 '13 at 03:43
  • What have you considered or tried? Does funcB wait for items which may not respond or is some computation which may take a long time? If it is something where it may not return, then your option is as dasblinkinglight states: is to place it in a thread and stop the thread if it reaches the time limit. You can have the program sleep so funcB gets resources to run. – Glenn Jan 06 '13 at 03:46
  • 1
    Also, bear in the mind the consequences of killing the thread. Will there be any side-effects to aborting early? – Lee Taylor Jan 06 '13 at 03:49

3 Answers3

2

One way to do this is to measure the time elapsed within funcB() since entering, e.g. if you have a loop in funcB(). Ideally, your function returns a value that indicates success or early termination, so funcA() has a way to know if funcB() completed.

Another way is to run funcB() in its own thread. If your main thread determines that 5 min have passed, it can terminate the thread that is executing funcB().

s.bandara
  • 5,636
  • 1
  • 21
  • 36
0

If you are a beginner, why not create a new process by using fork(). Then start timing within the new process and then terminate the process if it exceeds 5 minutes. Even though in most cases threads are better to use, it's easier for beginners to create a new process.

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
0

You should code a feature in funcB so it will know to return.. Anything else is somewhat unsafe, if funcB for example writes to files. There are many ways to do this. I'm assuming you have a loop in funcB, which you want to abort. If you have something else, like blocking IO operation, you have to do this a bit differently.

If you have single thread, you could code the abort logic directly into funcB. You could also give function pointer argument for funcB, which then calls the function every loop round to see if it could abort. That way you can more easily have different abort conditions.

If you have multiple threads, you should use an atomic flag variable, which you set from other thread when you want funcB to abort, and in funcB loop you then test it. This is the most common way to twll orher thread running a loop to quit.

Addition: If you are going to abort file operations, it's possible to do it safely if you do a few things:

  1. The function must do writes to temporary file (at same disk partition), and then close it and use rename operation (which is atomic at OS level when files are in same partition) to create/replace the final file. That way, if operation is aborted, the final file will not be left in corrupted state.

  2. The caller must have the file handle or file descriptor of the temporary file, so it can close the file if file operation was aborted while file was open. If file operation happens in another process, which is killed, then that will close all file handles and this is not needed.

  3. Caller should also always try to remove the temp file, in case the aborted function/thread/process did not have a chance to rename it to final name or remove it.

Addition 2: In Unix/Linux you can use alarm() function. See this: Simple Signals - C programming and alarm function

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176
  • Thank you for your great post. And I want to write to files in funcB(). In this case, do you have any other suggestion? :) – user1952216 Jan 07 '13 at 03:36
  • @user1952216 to answer that, more details are needed. Single thread? Multiple threads ok? Multiple processes ok? But, I'll add something about safe way to abort file handling. – hyde Jan 07 '13 at 08:02
  • hi, thank you for your comment. I need this in a multiple threads.what other information do you need? :) – user1952216 Jan 16 '13 at 09:25