threadpool中mFullCond条件变量并没有作用?

21 views
Skip to first unread message

浪迹天涯

unread,
Oct 28, 2008, 2:04:03 AM10/28/08
to spserver
if( mIndex < mTotal )
{
sp_syslog( LOG_NOTICE, "[tp@%s] waiting for %d thread(s) to finish
\n", mTag, mTotal - mIndex );
sp_thread_cond_wait( &mFullCond, &mMainMutex );
}
这个m_FullCond有可能在threadpool销毁前就已经触发,所以我觉得这里并没有起到真正的作用,删除这段也不会有什么影响。因为如果线
程在wait,触发后会判断线程池销毁信号;如果在忙do func,那么fucn结束后也会判断线程池销毁信号,所以并不影响线程工作时被强制终
止。
所以我想这个m_FullCond还有存在的必要吗?

stephen.nil

unread,
Oct 28, 2008, 1:17:25 PM10/28/08
to spse...@googlegroups.com
Hi, 浪迹天涯

mFullCond 是用于表示 线程池的线程 是否已经全部都空闲了。

只有在所有线程都已经空闲的情况下,再来做余下的清理工作显得更容易控制。


Best regards,

stephen.nil
2008-10-29

stephen.nil

unread,
Oct 28, 2008, 1:17:25 PM10/28/08
to spse...@googlegroups.com
Hi, 浪迹天涯

mFullCond 是用于表示 线程池的线程 是否已经全部都空闲了。

只有在所有线程都已经空闲的情况下,再来做余下的清理工作显得更容易控制。


Best regards,

stephen.nil
2008-10-29

>>>

浪迹天涯

unread,
Oct 28, 2008, 10:40:44 PM10/28/08
to spserver
谢谢回复。
我明白是这个作用,但是因为有可能某段时间所有线程都已空闲,然后触发了这个信号,但线程池并没有销毁,而是又有很多任务分发进来,而这时要销毁线程池
的话,sp_thread_cond_wait( &mFullCond, &mMainMutex )这里根本不能wait到真正的所有线程空闲信
号,而是刚才已经触发的信号。因为第一次信号触发后并没有wait成功,所以状态并未改变。

lau stephen

unread,
Nov 3, 2008, 9:13:01 PM11/3/08
to spse...@googlegroups.com
pthread_cond_signal 的 manpage 中的说法

The pthread_cond_signal() call unblocks at least one of the
threads that are blocked on the specified condition variable
cond (if any threads are blocked on cond).

The pthread_cond_signal() and pthread_cond_broadcast() func-
tions have no effect if there are no threads currently
blocked on cond.

按 manpage 的说法,如果之前没有线程在 wait 的话,那么 signal 调用就等于是空操作。
这个 signal 并不会被保留,再被稍后产生的 wait 调用捕获。

2008/10/29 浪迹天涯 <qj-qi...@sohu.com>:

浪迹天涯

unread,
Nov 5, 2008, 2:39:01 AM11/5/08
to spserver
在linux平台下的确是这样的。但是在win32平台下,是用SetEvent来模拟pthread_cond_signal(),这里面有个差
异:
1、pthread_cond_signal()如果没有线程wait的话,是直接返回的;
2、SetEvent的实现:没有线程等待这个Event时,它仍然会设置SignalState(见用红色高亮的代码)。
具体可参见如下文章:
http://blog.csdn.net/absurd/archive/2006/11/21/1402433.aspx

lau stephen

unread,
Nov 5, 2008, 2:47:18 AM11/5/08
to spse...@googlegroups.com
谢谢!这个参考资料很有用。

我要重新考虑一次在 win32 平台下的实现方法才行

2008/11/5 浪迹天涯 <qj-qi...@sohu.com>:

Reply all
Reply to author
Forward
0 new messages