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
2010-01-29 04:29:12
Thanks! That's exactly what I was looking for.
dunecat
2010-01-29 04:30:00
+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
2010-01-29 04:30:06
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
2010-01-29 04:45:50
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
2010-01-29 05:10:29
i dont see how above code are "non-blocking" call.the condition evaluation only happen after functions() returns...
YeenFei
2010-01-29 06:26:14
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
2010-01-29 07:09:39