multithreading problem in C++
in gamespy, it can ping up to 64 servers simultaneously. It uses multithreading, 1 thread per ping but the CPU utilization is close to nothing. I tried to follow the same pattern but my cpu utilization stays at 100 until all the pings are done. I made all the ping threads background threads and putting the main thread to sleep until all the worker threads were done but it still stays at 100% cpu utilization while pinging.
Can anyone help?
I''m just trying to make a d2 game finder
1. ping all possible ips of my range. add ones that ping in 10ms or less to a file
2. loop the file connection to port 4000
3. trigger event when a connection to port 4000 has been made.
here is what the worker threads look like -> http://www.hardforums.com/showthread.php?s=dc11d4c3902b1593cc7258312974ee17&threadid=365328
--------------------------
One problem with the programmer''''s mentality is insecurity. This goes deep. An insulting college litany says that failed mathematicians become computer programmers. They are also ridiculed for being nerdy losers, for being too fat or too skinny, and for having few social skills. Most programmers can be spotted easily in a crowd. Nobody really wants to hang out with them. Put thousands of these people in one company and if you can get them to work, you become a billionaire. -John C. Dvorak
--------------------------One problem with the programmer''s mentality is insecurity. This goes deep. An insulting college litany says that failed mathematicians become computer programmers. They are also ridiculed for being nerdy losers, for being too fat or too skinny, and for having few social skills. Most programmers can be spotted easily in a crowd. Nobody really wants to hang out with them. Put thousands of these people in one company and if you can get them to work, you become a billionaire. -John C. Dvorak
are you using polling or blocking in those pinging threads ?
if you poll then it will be 100% even with one thread
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
if you poll then it will be 100% even with one thread
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
inside the pingfunction looks like this
timer->start()
sendto(IP)
while(//there is no data available to recv OR timer has not elapsed)
recvfrom()//if there is data available
but if the timer elapsed, it does call recvfrom()
it closes the socket.
do you think its the while loop that's killing it?
it's .net, I believe the default mode is blocking
[edited by - HJB417 on April 1, 2002 4:26:13 AM]
timer->start()
sendto(IP)
while(//there is no data available to recv OR timer has not elapsed)
recvfrom()//if there is data available
but if the timer elapsed, it does call recvfrom()
it closes the socket.
do you think its the while loop that's killing it?
it's .net, I believe the default mode is blocking
[edited by - HJB417 on April 1, 2002 4:26:13 AM]
--------------------------One problem with the programmer''s mentality is insecurity. This goes deep. An insulting college litany says that failed mathematicians become computer programmers. They are also ridiculed for being nerdy losers, for being too fat or too skinny, and for having few social skills. Most programmers can be spotted easily in a crowd. Nobody really wants to hang out with them. Put thousands of these people in one company and if you can get them to work, you become a billionaire. -John C. Dvorak
you will need to give more code then that
but most likely the socket is not blocking.....especaially since you have the timer thing going....does it kick out of the loop when the timer is up ? IF so then it is not blocking.
IF it is blocking then is would stay at recvfrom() until there is data to be received or their was an error on the socket like not connected or invalid socket or something like that
it would act like it was trying to execute the recvfrom() function and never returning from it until you get data if it is blocking.
And if it isnt blocking then that is what is eating all the cpu time
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
but most likely the socket is not blocking.....especaially since you have the timer thing going....does it kick out of the loop when the timer is up ? IF so then it is not blocking.
IF it is blocking then is would stay at recvfrom() until there is data to be received or their was an error on the socket like not connected or invalid socket or something like that
it would act like it was trying to execute the recvfrom() function and never returning from it until you get data if it is blocking.
And if it isnt blocking then that is what is eating all the cpu time
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
http://www.skidmore.edu/~h_blackw/Ping.cpp
It was bad programming from the start =p
the while loop is what killed it.
There's a SetSocketOptions method within the socket class that allows me to set a Send/Recv timeout value. Setting that value did the trick and allowed me to get rid of the while loop and the timer
=)
Thx for your help though.
[edited by - HJB417 on April 1, 2002 5:31:34 AM]
It was bad programming from the start =p
the while loop is what killed it.
There's a SetSocketOptions method within the socket class that allows me to set a Send/Recv timeout value. Setting that value did the trick and allowed me to get rid of the while loop and the timer
=)
Thx for your help though.
[edited by - HJB417 on April 1, 2002 5:31:34 AM]
--------------------------One problem with the programmer''s mentality is insecurity. This goes deep. An insulting college litany says that failed mathematicians become computer programmers. They are also ridiculed for being nerdy losers, for being too fat or too skinny, and for having few social skills. Most programmers can be spotted easily in a crowd. Nobody really wants to hang out with them. Put thousands of these people in one company and if you can get them to work, you become a billionaire. -John C. Dvorak
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement