Link to home
Create AccountLog in
C++

C++

--

Questions

--

Followers

Top Experts

Avatar of tmar89
tmar89

using select() as a timer
I want to use select() as a timer to block for 10us in a loop so I can perform some code on predetermined intervals.  So far I have

#include <stdio.h>
#include <winsock2.h>

int main()
{
     struct timeval tv;
 
     tv.sec = 0;  
     tv.usec = 10;  

     for(;;) {
          // Wait 10 us
          select(0,NULL,NULL,NULL,&tv);
          // do some code
     }
}

the select always sees it as non-blocked and goes right through with an error of -1.  I've never used this before so I could use some help please.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of jkrjkr🇩🇪

>>the select always sees it as non-blocked and goes right through with an error of -1

'0' is aninvalid socket file descriptor, so this certainly won't work.

Avatar of jkrjkr🇩🇪

BTW, 10µs is way beyond any resolution Windows gan give you - see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Timers/AboutTimers.asp ("About Timers") for more information.

Avatar of stefan73stefan73🇩🇪

tmar89,

that's not a good & stable way to delay something. Use sleep() instead. Or, if you want to wait for a really small period, consider something like yield().

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of stefan73stefan73🇩🇪

High-resultion lternative for a single-CPU system:

1. Measure the number of CPU cycles in a certain (rather long) time span (i.e., sleep(1)).
2. Calculate how many cycles you need for a very small time span (say, 10us).
3. Poll until you reached your target cycle count.

(Use rdtsc to get the current counter in EAX/EDX).

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelseitsmeandnobodyelse🇩🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of stefan73stefan73🇩🇪

Alex,

Hmm, man page says:
     The select() function supports regular files,  terminal  and
     pseudo-terminal  devices,  STREAMS-based  files,  FIFOs  and
     pipes. The behavior of select()  on  file  descriptors  that
     refer to other types of file is unspecified.

A socket is in neither category, I'd say.

And that's not what he was looking for.

Stefan,

that function i gave him runs in a 365 days, 24h environment on NT, AIX, OS/2 and VMS. So, it is proved, that it works with sockets and is portable code.

MSDN says for the second parameter:

readfds
[in/out] An optional pointer to a set of sockets to be checked for readability.

I couldn't think that (LINUX??) man pages regarding socket API will not have a similar description.

With select(..) you may check a set of sockets on their status. The function above only checks one socket for read status (i. e. if a recv() call wouldn't block). To be able to use the function you have to set the socket to non-blocking.

Regards, Alex
 

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of tmar89tmar89

ASKER

What I am looking for is a reliable way to perform commands at a very high determined speed.  I was told to look at select() because it could generate the hold and wait feature I wanted.  When I tried using WM_TIMER in MFC and _sleep() at 1 millisecond, I would only get 62 or so calls per second actually occuring, not the 1000 that I want.  Any my code is very simple where it is copying data from a memory address to another address.  Nothing taxing on the system.   So for instance I tried this:

while (i < 1000) {
    // read from memory
    sleep(1);
    ++i;
}

This took much longer than a second to perform.  I did a similar idea with WM_TIMER where I set the timer to 1 ms and wrote very similar code in the TIMER function and it performed basically the same with about 62 calls per second.  I guess this isn't a reliable way to perform tasks at specific times and this is why I was told to look at select() but I'm afraid that it won't work correctly either.  With select(), I just want a "dummy" file descriptor for the socket.   Can anyone actually think of another way to do this and take into consideration this is on a windows XP machine

select() is a reliable function to check one or a set of read sockets, if there is anything to read. You can have a zero timeout or a very small timeout (it depends on OS what minimum is) and the problem might be, that a loop will cost too much CPU time (if you haven't something to do in the meantime).

Another possibility is to use an own thread for each read socket and notify a main thread by callback function. However, to be able to close the read thread from main thread smoothly (not by killing it), you would need a select() call in the reading thread in order to check an exit flag. But a timeout for this could be 1 second or more.

Tell me if you need an example for this.

Regards, Alex

C++

C++

--

Questions

--

Followers

Top Experts

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.