pausing or waiting in c

bigalt

Golden Member
Oct 12, 2000
1,525
0
0

do i just want to be doing something like:

t1 = clock();
t2 = clock();
while ((t2-t1)/CLOCKS_PER_SEC<.01) t2 = clock();




or is there a more elegant way of pausing for a set amount of time?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Depends on the OS. You might be able to sleep() or in Windows you can set a timer which fires a message.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: DaveSimmons
Depends on the OS. You might be able to sleep() or in Windows you can set a timer which fires a message.

Windows will also acknowledge Sleep from C or C++.

I believe that it may be a C standard.

 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
Originally posted by: EagleKeeper
Originally posted by: DaveSimmons
Depends on the OS. You might be able to sleep() or in Windows you can set a timer which fires a message.

Windows will also acknowledge Sleep from C or C++.

I believe that it may be a C standard.

nope. not part of the standard, but sleep(int seconds) is common in unix (unistd.h)
in windows, Sleep(millisecs) serves a similar purpose (windows.h)

 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
POSIX specifies nanosleep() for fine-grained delays. Probably you can use usleep(), which is simpler to use than nanosleep(), but is defined (and differently at that) by BSD 4.3 and SUSv2.

sleep() is POSIX too, but it only works on whole seconds.

clock() only measures processor time for the process it runs under. Or at least that's what the spec says. Still, it's the only ANSI function that measures with the precision you look like you want. Everything else is a syscall.

Timers appear to be the Windows option. I wouldn't know if any of the above work for it (probably not).
 

bigalt

Golden Member
Oct 12, 2000
1,525
0
0
well the sleep() function doesn't seem to fly with my visual c 6.0 under windows.h.

if the clock function only counts for the current process that might be kind of awkward considering I have to have multiple threads.

Maybe someone can suggest a better way to do this:

We've got a PC running windows connected to a robotic arm which wants 6 variables every 12 ms (more or less). It also wants a UDP connection kept alive the whole time (hence the separate thread.

These variables are changes in position and angle. Unfortunately the experiment we're running can only figure out these numbers ever 1 second or so.

So what my current plan is, keep this UDP process running, and have it feed off a global set of variables. I want it to send every 10 ms or so and then reset the variables to zero, so it sends that change once and no more.

I'd rather have it miss the variable and have to repeat the step a second later than have it overshoot. Neither is that big a deal, so precision isn't too much of an issue.

So my thought was to have my send loop delay about 10 ms each time. Sound reasonable?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Sounds like you need to use this:

The SetTimer function creates a timer with the specified time-out value.

UINT_PTR SetTimer(
HWND hWnd, // handle to window
UINT_PTR nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // timer procedure
) ;

as essentially a 10 ms "hearbeat" interrupt generator then have a message handler for the timer messages. See details at MSDN or in your books online
 

bigalt

Golden Member
Oct 12, 2000
1,525
0
0
I forgot to mention that I'm more of a recycler than a coder, so I'm working off of code which I don't fully understand.

The timer looks like generally what I want to do, but it would take some rewriting. Basically any for/if/whiles that operate on the timer would need to be transferred to a function which would serve as my TimerFunc? and if I have no idea what a window is, how would I treat that handle?

Or up another tree, if neither my windows.h or my winuser.h seem to have the sleep function, what's the best remedy for that? Can I just download one from somewhere else with no problems?

edit: grr... i can't find settimer either. is there a place i'm supposed to update my windows.h file?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: bigalt
edit: grr... i can't find settimer either. is there a place i'm supposed to update my windows.h file?
" Header: Declared in Winuser.h; include Windows.h." <-- The Friendly Manual (Books Online or MSDN)

and if I have no idea what a window is, how would I treat that handle?
If you don't know the Windows event-driven model where "messages" sent to "windows" control program flow, and don't want to work through Petzold's Programming Windows to learn it, then just keeping your current polling loops might be the way to go.
 

bigalt

Golden Member
Oct 12, 2000
1,525
0
0
Originally posted by: DaveSimmons
Originally posted by: bigalt
edit: grr... i can't find settimer either. is there a place i'm supposed to update my windows.h file?
" Header: Declared in Winuser.h; include Windows.h." <-- The Friendly Manual (Books Online or MSDN)

and if I have no idea what a window is, how would I treat that handle?
If you don't know the Windows event-driven model where "messages" sent to "windows" control program flow, and don't want to work through Petzold's Programming Windows to learn it, then just keeping your current polling loops might be the way to go.

sorry I'm so slow, and thanks for your time :)

The windows.h I have doesn't seem to have the sleep included though.

That is, I have done an #include <windows.h> (with and without the capital)

and the compiler still doesn't recognize sleep(.01);

edit: omfg I'm retarded, it was Sleep() and not sleep()

re-edit: It does take some practice to be able to digest the friendly manual! thanks again!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Again in The Friendly Manual:

"VOID Sleep(
DWORD dwMilliseconds // sleep time
) ;

...

Header: Declared in Winbase.h; include Windows.h. '

Your error could come from using _s_leep instead of _S_leep, or from using a double instead of a DWORD (which is a 32-bit int)
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
From MSDN Manual
Sleep
The Sleep function suspends the execution of the current thread for a specified interval.

VOID Sleep(
DWORD dwMilliseconds // sleep time in milliseconds
);

Parameters
dwMilliseconds
Specifies the time, in milliseconds, for which to suspend execution. A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. A value of INFINITE causes an infinite delay.
Return Values
This function does not return a value.

Remarks
A thread can relinquish the remainder of its time slice by calling this function with a sleep time of zero milliseconds.

You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.