Skip to content

Commit 2d96dca

Browse files
committed
增强线程调试。
1 parent e47fbeb commit 2d96dca

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

kbe/src/lib/thread/threadpool.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ currentFreeThreadCount_(0),
116116
normalThreadCount_(0),
117117
isDestroyed_(false)
118118
{
119-
120-
121119
THREAD_MUTEX_INIT(threadStateList_mutex_);
122120
THREAD_MUTEX_INIT(bufferedTaskList_mutex_);
123121
THREAD_MUTEX_INIT(finiTaskList_mutex_);
@@ -138,9 +136,27 @@ bool ThreadPool::initializeWatcher()
138136
WATCH_OBJECT((boost::format("%1%/normalThreadCount") % name()).str().c_str(), this->normalThreadCount_);
139137
WATCH_OBJECT((boost::format("%1%/bufferedTaskSize") % name()).str().c_str(), this, &ThreadPool::bufferTaskSize);
140138
WATCH_OBJECT((boost::format("%1%/finiTaskSize") % name()).str().c_str(), this, &ThreadPool::finiTaskSize);
139+
WATCH_OBJECT((boost::format("%1%/busyThreadStates") % name()).str().c_str(), this, &ThreadPool::printThreadWorks);
141140
return true;
142141
}
143142

143+
//-------------------------------------------------------------------------------------
144+
std::string ThreadPool::printThreadWorks()
145+
{
146+
std::string ret;
147+
148+
THREAD_MUTEX_LOCK(threadStateList_mutex_);
149+
150+
std::list<TPThread*>::iterator itr = busyThreadList_.begin();
151+
for(; itr != busyThreadList_.end(); itr++)
152+
{
153+
ret += (boost::format("%1%:(%2%), ") % (*itr) % (*itr)->printWorkState()).str();
154+
}
155+
156+
THREAD_MUTEX_UNLOCK(threadStateList_mutex_);
157+
return ret;
158+
}
159+
144160
//-------------------------------------------------------------------------------------
145161
void ThreadPool::finalise()
146162
{

kbe/src/lib/thread/threadpool.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,27 @@ class TPThread
190190
设置本线程要处理的任务
191191
*/
192192
INLINE ThreadPool* threadPool();
193+
194+
/**
195+
输出线程工作状态
196+
主要提供给watcher使用
197+
*/
198+
std::string printWorkState()
199+
{
200+
char buf[256];
201+
lock();
202+
sprintf(buf, "task=%x,state=%d", currTask_, state_);
203+
unlock();
204+
return buf;
205+
}
193206
protected:
194207
THREAD_SINGNAL cond_; // 线程信号量
195208
THREAD_MUTEX mutex_; // 线程互诉体
196-
int threadWaitSecond_; // 线程空闲状态超过这个秒数则线程退出 小于0为永久线程 秒单位
209+
int threadWaitSecond_; // 线程空闲状态超过这个秒数则线程退出, 小于0为永久线程(秒单位)
197210
TPTask * currTask_; // 该线程的当前执行的任务
198211
THREAD_ID tidp_; // 本线程的ID
199212
ThreadPool* threadPool_; // 线程池指针
200-
THREAD_STATE state_; // 线程状态 -1还未启动, 0睡眠 1繁忙中
213+
THREAD_STATE state_; // 线程状态: -1还未启动, 0睡眠, 1繁忙中
201214
};
202215

203216

@@ -214,6 +227,11 @@ class ThreadPool
214227

215228
bool hasThread(TPThread* pTPThread);
216229

230+
/**
231+
获取当前线程池所有线程状态(提供给watch用)
232+
*/
233+
std::string printThreadWorks();
234+
217235
/**
218236
获取当前线程总数
219237
*/

0 commit comments

Comments
 (0)