Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WaitForSingleObject cpu usage...

1,017 views
Skip to first unread message

Peter Salom

unread,
Oct 3, 2003, 5:27:10 AM10/3/03
to
Hi!

I need to otimize performance of a piece of C++ code (ActiveX)
I'm using sockets and is use to _select() (in blocking mode) to wait
incomming data.

Profiling this component, i see the most CPU usage is in ... select()
call!!!
I can see a lot of time spent in call to WaitForSingleObject() !!!

Is WaitForSingleObject really using a lot of CPU?

There is another way to make a blocking usage of sokets?

Thank you in advance!

Pet.

Slava M. Usov

unread,
Oct 3, 2003, 7:30:47 AM10/3/03
to
"Peter Salom" <ps...@hotmail.com> wrote in message
news:#G912BZi...@TK2MSFTNGP11.phx.gbl...

> I need to otimize performance of a piece of C++ code (ActiveX)
> I'm using sockets and is use to _select() (in blocking mode) to wait
> incomming data.

Do not use select(). Use IO completion ports.

S

Mike Deakins

unread,
Oct 4, 2003, 1:27:20 AM10/4/03
to
WaitForSingleObject doesn't use a lot of CPU. It just blocks the calling
thread and release CPU for other threads. But from the Profiler's point of
view, that thread really executes for the certain period of time.

If you use ::Sleep(10000) to suspend execution for 10 seconds, the profiler
will report the Sleep costs 10 seconds.

--
Mike J. Deakins
For the shining star in my skies


"Peter Salom" <ps...@hotmail.com> wrote in message

news:%23G912BZ...@TK2MSFTNGP11.phx.gbl...

Peter Salom

unread,
Oct 5, 2003, 10:33:46 AM10/5/03
to
Mike,

thanks, but...
I think the same about WaitForSingleObject(); it releases CPU to other
threads, but when i open TaskManager or Profiler, my object still eating CPU
time in select() call!!!

If really WaitForSingleObject doesn't use CPU, then the problem is bigger!
I can't use TaskManger/Profiler/Vtune to my waiting multihread app!!!

microsoft: What kind of tool i can use to measure "REAL" performance/CPU
usage of my mutihtreaded application?

Thank you,

Pet.


"Mike Deakins" <miked...@nospam.hotmail.com> wrote in message
news:unvr1gji...@TK2MSFTNGP11.phx.gbl...

Slava M. Usov

unread,
Oct 5, 2003, 11:01:25 AM10/5/03
to
"Peter Salom" <ps...@hotmail.com> wrote in message
news:eE2Pn20i...@TK2MSFTNGP10.phx.gbl...

[...]

WaitForSingleObject() does not use a lot of CPU. It is select() that uses a
lot of CPU. Consider this code:

while (1) WaitForSingleObject(object, 0);

Any profiler will tell you that the CPU time is spent in
WaitForSingleObject(). Does that mean that WaitForSingleObject() is bad? No,
it means that the code that calls it is bad. The same applies to select().
Do not use it.

S


Peter Salom

unread,
Oct 5, 2003, 11:46:36 AM10/5/03
to
if really select() uses something like "while (1)
WaitForSingleObject(object, 0);" i will never user select() again!! what
kind of shit!


"Slava M. Usov" <stripit...@gmx.net> wrote in message
news:es5aNG1i...@TK2MSFTNGP12.phx.gbl...

Arnaud Debaene

unread,
Oct 5, 2003, 12:43:14 PM10/5/03
to
Peter Salom wrote:
> Mike,
>
> thanks, but...
> I think the same about WaitForSingleObject(); it releases CPU to other
> threads, but when i open TaskManager or Profiler, my object still
> eating CPU time in select() call!!!
>
> If really WaitForSingleObject doesn't use CPU, then the problem is
> bigger! I can't use TaskManger/Profiler/Vtune to my waiting
> multihread app!!!

Your app is multithreaded? Are you sure that it isn't the others threads of
you app that are eating CPU during your select call? Try to debug-break in
your app while it is eating the CPU and see where the various threads are.
You can also use perfmon to watch the "% Processor Time" counter for *each*
of your threads.

Arnaud
MVP - VC


Peter Salom

unread,
Oct 5, 2003, 12:47:59 PM10/5/03
to
Yes, my app is multithread, but i'm sure. Is calling select() when is uses
lot ot CPU.

"Arnaud Debaene" <adeb...@club-internet.fr> wrote in message
news:eomg3%231iDH...@TK2MSFTNGP09.phx.gbl...

Slava M. Usov

unread,
Oct 5, 2003, 3:30:52 PM10/5/03
to
"Peter Salom" <ps...@hotmail.com> wrote in message
news:Ok8NUf1i...@tk2msftngp13.phx.gbl...

> if really select() uses something like "while (1)
> WaitForSingleObject(object, 0);" i will never user select() again!! what
> kind of shit!

I hope it is not _so_ bad. But it is rather inefficient. Consider IO
completion ports instead.

S


Sami Korhonen

unread,
Oct 5, 2003, 10:35:31 PM10/5/03
to
Sounds like you have initialized fd_set wrong & one of the listed
descriptors causes select() to return immeaditely. Or that program is
polling for writability/readability while it isn't writing/reading anything
to/from sockets.

"Peter Salom" <ps...@hotmail.com> wrote in message

news:eFqOpB2i...@TK2MSFTNGP12.phx.gbl...

arkadyf

unread,
Oct 7, 2003, 7:00:10 AM10/7/03
to
You cannot use select() ( you can but any case socket blocked before ) , if
you want to use select() do socket non-blocked and after select you can
return it back to blocked
Arkady

"Slava M. Usov" <stripit...@gmx.net> wrote in message

news:eWL9oHai...@TK2MSFTNGP11.phx.gbl...

0 new messages