Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to create non blocking functions

Started by hahaha Aug 9, 2007 at 4:37 AM 2 replies 1k views
Original Post
hahaha
hahaha
Using c++ and windows is there a way to create a non blocking function eg

void NonBlocking(void)
{
    Sleep(5000);
    cout << "Waiting done..." << "\n";
}

I want to be able to call this function without it blocking. Sort of like a timer but i dont want to have to create a new thread or create a win32 timer or something Thanks
TheUnbeliever
TheUnbeliever
No, it must continue in another thread (either created by yourself or by some third party API or library).

What you want to do is tell something to wait 5 seconds and call you back. What you're actually doing is telling the thread to sleep for at least 5 seconds and then print a message. If you're going to tell some thread to sleep for 5 seconds and want it to be non-blocking, it has to be a thread other than the calling one.

Also, drop the (void) in the parameters. That's a C thing, and is unnecessary ugliness in C++.
[TheUnbeliever]
mrcheesewheel
mrcheesewheel
I agree... and I'd also say that creating a (one shot) win32 timer callback provides exactly this functionality. Launching another thread is not a complicated job per se... but the thread itself must call your callback at an appropriate moment (ie it could call mid function) and you are thus required to handle any thread synchronisation issues appropriately. If you use a windows callback, it will callback at an appropriate time (ie when it's time comes in the event queue) so synchronisation (at a sub function level) is unnecessary.
Hope this helps,

Dan
hahaha
hahaha
Thanks but the reason i dont want to create another thread with win32 is for some reason after createing a thread, calls to ShowCursor are not working. Nothin happens when i call it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.