tags:

views:

77

answers:

2

I have a function that runs in C. I would like for it to timeout, or at least be non blocking. Is there a way to do that without running it as a thread?

+3  A: 

select() (or one of its platform-specific equivalents) is what should be used if you don't know that there is input available from a blocking file or socket, and want to continue if there isn't.

Ignacio Vazquez-Abrams
Thanks! That's exactly what I was looking for.
dunecat
+1  A: 

You can make it nonblocking simply by making no blocking calls inside your function. If you want it to time out, just wrap the non-blocking calls you're making in a little bit of code that gets and checks the time elapsed since the function was called.

Carl Norum
By "a little bit of code" mean you "put it in a thread and have an outside thread monitor it and kill it if required", right?
khedron
Depends, I guess. If you're only making nonblocking calls, you don't need extra threads. By "a little bit of code" I meant something along the lines of `startTime = currentTime(); do { functions(); } while ((currentTime() - startTime) < timeout);`
Carl Norum
i dont see how above code are "non-blocking" call.the condition evaluation only happen after functions() returns...
YeenFei
If `functions()` is nonblocking, that code is nonblocking. The original question was pretty vague; I think @Ignacio figured out what was being asked a lot better than I did.
Carl Norum