C++
--
Questions
--
Followers
Top Experts
#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,&t
// 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.
'0' is aninvalid socket file descriptor, so this certainly won't work.
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().






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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).
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.
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

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.
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
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++
--
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.