windows下多线程使用Sleep循环检测与WaitForSingleObject一个event哪个更耗费资源?或者有没有更好的方法?

214 views
Skip to first unread message

aking

unread,
Mar 31, 2008, 5:19:36 AM3/31/08
to dev4s...@googlegroups.com
大家好
    我遇到这样一个问题,
    windows环境下,需要一组4个线程同时等待一个事件后作一些操作,
    而这样一组一组的线程是多组的,不同组的线程等待不同的事件
    一种是Sleep等待,一种是WaitForSingleObject
    哪种方式好些呢?或者有没有更好的办法,
   我测试了两种方式感觉都挺耗资源的,20组左右,CPU占用率到了50-60%
   本人新手,请各位指点讨论
方案A
iThreadCount = 0;
iOK = 0;

Thread :
     FuncA();
     While(iOK!=1)
     {
        Sleep(100);
    }

     dosomthing();

FuncA()
{
    InterlockedIncrement(iThreadCount) ;
    if(iThreadCount >= 4)
   {
      iThreadCount = 0;
      InterlockedIncrement(iOK ) ;
   }
}


方案B
iThreadCount = 0;
HANDLE  hEvent;

Thread :
     FuncB();
    WaitForSingleObject(hEvent);
     dosomthing();

FuncB()
{
    InterlockedIncrement(iThreadCount) ;
    if(iThreadCount >= 4)
   {
      iThreadCount = 0;
      SetEvent(hEvent);    
   }
}
--
I am aking

sunway

unread,
Mar 31, 2008, 6:44:37 AM3/31/08
to 高性能网络编程邮件列表
用了event何必用轮询?

aking

unread,
Mar 31, 2008, 8:43:30 AM3/31/08
to dev4s...@googlegroups.com
event效率要高一些?
第一个我用的是轮询
第二个我用的是event

可是我发现system进程的CPU占用率很高到50%,在开一组的情况下

在08-3-31,sunway <sunh...@gmail.com> 写道:

aking

unread,
Mar 31, 2008, 12:21:04 PM3/31/08
to dev4s...@googlegroups.com
晕,看了下system进程
原来以为是因为event是内核资源占cpu
仔细一看竟然是装的冰点占了50%左右的cpu
教训
waitforsingleobjec event 100个以内应该占不了多少cpu吧?

在08-3-31,aking <kang...@gmail.com> 写道:



--
I am aking

Terry

unread,
Mar 31, 2008, 9:46:42 PM3/31/08
to 高性能网络编程邮件列表
WaitForSingleObject
会比你不断的Sleep()效率要高,一个是用户级的,一个是内核级的.当然内核级切换到用户级时所要的开销会比较大而已.

去Wait 一百个 Event 应该不会占用很多CPU的.

aking

unread,
Mar 31, 2008, 11:37:22 PM3/31/08
to dev4s...@googlegroups.com
恩看来我还是用event好了

在08-4-1,Terry <jxd...@gmail.com> 写道:

6spring

unread,
Apr 1, 2008, 8:24:23 AM4/1/08
to 高性能网络编程邮件列表
其实也可以
CreateIoCompletionPort *1
GetQueuedCompletionStatus *N ( N>=4 )
符合条件时PostQueuedCompletionStatus *4 (Post 4次是为了激活4个thread)

这样20组不需要80个thread
> --
> I am aking

关中刀客

unread,
Apr 1, 2008, 11:17:15 PM4/1/08
to 高性能网络编程邮件列表
恩,使用iocp来实现内存池,很不错
> > I am aking- 隐藏被引用文字 -
>
> - 显示引用的文字 -

sunway

unread,
Apr 2, 2008, 12:44:49 AM4/2/08
to 高性能网络编程邮件列表
晕。。。
> > - 显示引用的文字 -- 隐藏被引用文字 -
>
> - 显示引用的文字 -

关中刀客

unread,
Apr 2, 2008, 1:42:43 AM4/2/08
to 高性能网络编程邮件列表
写错了,是使用iocp来管理一个线程池

Zhe Ma

unread,
Apr 2, 2008, 2:47:53 AM4/2/08
to dev4s...@googlegroups.com
sleep是一个busy waiting, 任何程序使用busy waiting效率都不会太高
类似linux的spin-lock情形例外

在08-4-2,关中刀客 <guanzho...@gmail.com> 写道:

wk

unread,
Apr 3, 2008, 11:55:27 PM4/3/08
to dev4s...@googlegroups.com
在 OS 中, sleep当然不是spin-lock like 的 busy waiting,

Zhe Ma 写道:

sunway

unread,
Apr 4, 2008, 9:14:26 AM4/4/08
to 高性能网络编程邮件列表
在WIN下,sleep()会把调用线程移出运行队列,超时以后再次加入运行队列.其实和WaitForSingleObject区别不是很大.

On 4月4日, 上午11时55分, wk <comet...@163.com> wrote:
> 在 OS 中, sleep当然不是spin-lock like 的 busy waiting,
>
> Zhe Ma 写道:
>
>
>
> > sleep是一个busy waiting, 任何程序使用busy waiting效率都不会太高
> > 类似linux的spin-lock情形例外
>
> > 在08-4-2,关中刀客 <guanzhongda...@gmail.com> 写道:

aking

unread,
Apr 5, 2008, 9:48:44 PM4/5/08
to dev4s...@googlegroups.com
呵呵,6spring让我长见识了
完成端口也是可以这么用的

在08-4-2,关中刀客 <guanzho...@gmail.com> 写道:



--
I am aking

aking

unread,
Apr 5, 2008, 10:00:00 PM4/5/08
to dev4s...@googlegroups.com
Sleep是不是实际对应一个定时器?

在08-4-2,Zhe Ma <mazhe...@gmail.com> 写道:



--
I am aking

aking

unread,
Apr 5, 2008, 10:03:50 PM4/5/08
to dev4s...@googlegroups.com
噢,那Sleep和SleepEx呢?

在08-4-4,sunway <sunh...@gmail.com> 写道:

sunway

unread,
Apr 6, 2008, 9:54:48 AM4/6/08
to 高性能网络编程邮件列表
sleepex会把等待线程设置为等待的警告状态,适合APC类的函数比如WSARecv()设置了回调函数.
只是WaitForM****函数的包装而已.
sleep也应该是WaitFor***的包装.

On 4月6日, 上午10时03分, aking <kangxi...@gmail.com> wrote:
> 噢,那Sleep和SleepEx呢?
>
> 在08-4-4,sunway <sunhui...@gmail.com> 写道:

tao yuan

unread,
Apr 6, 2008, 10:09:07 PM4/6/08
to dev4s...@googlegroups.com
atl里有一个用iocp做的线程池的封装,可以看看。Sleep主要的问题,还是灵活度不够把,
性能上差别应该不会太大。

在08-4-6,sunway <sunh...@gmail.com> 写道:

esx

unread,
Apr 10, 2008, 12:05:09 PM4/10/08
to 高性能网络编程邮件列表
sleep和wait本身的性能都差不太多。
楼主用sleep的主要问题在于轮询消耗了很多CPU
至于wait。。。你wait 10000个event也不会耗什么CPU,只有event被触发了才会开始干活。

aking wrote:
> ��Һ�
> ��������һ�����⣬
> windows�����£���Ҫһ��4���߳�ͬʱ�ȴ�һ���¼�����һЩ����
> ������һ��һ����߳��Ƕ���ģ���ͬ����̵߳ȴ�ͬ���¼�
> һ����Sleep�ȴ�һ����WaitForSingleObject
> ���ַ�ʽ��Щ�أ�������û�и�õİ취��
> �Ҳ�����}�ַ�ʽ�о�ͦ����Դ�ģ�20�����ң�CPUռ���ʵ���50-60%
> �������֣����λָ������
> ����A
> iThreadCount = 0;
> iOK = 0;
>
> Thread :
> FuncA();
> While(iOK!=1)
> {
> Sleep(100);
> }
>
> dosomthing();
>
> FuncA()
> {
> InterlockedIncrement(iThreadCount) ;
> if(iThreadCount >= 4)
> {
> iThreadCount = 0;
> InterlockedIncrement(iOK ) ;
> }
> }
>
>
> ����B

aking

unread,
Apr 10, 2008, 8:58:00 PM4/10/08
to dev4s...@googlegroups.com
这几天一直在弄这个
终于好了
经过比较这两种方式性能确实是相差不多的
这么一说Sleep(n) 应该和 WaitFor...(obj,n)差不多了

在08-4-11,esx <nay...@gmail.com> 写道:



--
I am aking

aking

unread,
Apr 10, 2008, 9:00:11 PM4/10/08
to dev4s...@googlegroups.com
呵呵 谢谢各位了:)

在08-4-11,aking <kang...@gmail.com> 写道:



--
I am aking
Reply all
Reply to author
Forward
0 new messages