Recommended Posts

I am working on a game in C, and it is (thus far) platform independent.
How do I cause a function to sleep until an interruption occurs, and how do I cause that interruption?
My current solution is as follows, but it seems a bit of a hack:
The 'interruption' is in the form of an external function changing a variable
The sleeping function calls usleep() for some small period of time, then checks the variable
If the variable has been modified, it breaks out of the loop
Is there a simple, platform independent way of doing this? I looked into posix threading, is this the direction I should be looking in? If so, does anyone have a brief, simple summary of how to achieve this sort of thing with POSIX threads? It can be assumed that the function sending the interrupt is on a separate thread than the sleeping function.
Thanks.

0

Share this post

Link to post

Share on other sites

I'm not privy to the POSIX thread library, but I guess you'll be using some sort of waitable condition / event variable. However, beware that this sort of thing can't really be done in a strictly platform-independent way. As soon as you start doing things involving threads / synchronization, true platform-indepedence goes out the window ... even when using a "platform-indepedent" wrapper such as the boost.threads library (but then that's a no-go anyway, as you explicitly stated that you're coding in C).