Skip to content

Commit 8444f07

Browse files
committed
set thread name with prctl().
1 parent c4988c9 commit 8444f07

5 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎muduo/base/Thread.cc‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <errno.h>
1616
#include <stdio.h>
1717
#include <unistd.h>
18+
#include <sys/prctl.h>
1819
#include <sys/syscall.h>
1920
#include <sys/types.h>
2021
#include <linux/unistd.h>
@@ -85,7 +86,8 @@ struct ThreadData
8586
ptid.reset();
8687
}
8788

88-
muduo::CurrentThread::t_threadName = name_.c_str();
89+
muduo::CurrentThread::t_threadName = name_.empty() ? "muduoThread" : name_.c_str();
90+
::prctl(PR_SET_NAME, muduo::CurrentThread::t_threadName);
8991
try
9092
{
9193
func_();
@@ -161,7 +163,7 @@ Thread::Thread(const ThreadFunc& func, const string& n)
161163
func_(func),
162164
name_(n)
163165
{
164-
numCreated_.increment();
166+
setDefaultName();
165167
}
166168

167169
#ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -173,7 +175,7 @@ Thread::Thread(ThreadFunc&& func, const string& n)
173175
func_(std::move(func)),
174176
name_(n)
175177
{
176-
numCreated_.increment();
178+
setDefaultName();
177179
}
178180

179181
#endif
@@ -186,6 +188,17 @@ Thread::~Thread()
186188
}
187189
}
188190

191+
void Thread::setDefaultName()
192+
{
193+
int num = numCreated_.incrementAndGet();
194+
if (name_.empty())
195+
{
196+
char buf[32];
197+
snprintf(buf, sizeof buf, "Thread%d", num);
198+
name_ = buf;
199+
}
200+
}
201+
189202
void Thread::start()
190203
{
191204
assert(!started_);

‎muduo/base/Thread.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Thread : boost::noncopyable
3939
static int numCreated() { return numCreated_.get(); }
4040

4141
private:
42+
void setDefaultName();
43+
4244
bool started_;
4345
bool joined_;
4446
pthread_t pthreadId_;

‎muduo/base/ThreadPool.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void ThreadPool::start(int numThreads)
3939
for (int i = 0; i < numThreads; ++i)
4040
{
4141
char id[32];
42-
snprintf(id, sizeof id, "%d", i);
42+
snprintf(id, sizeof id, "%d", i+1);
4343
threads_.push_back(new muduo::Thread(
4444
boost::bind(&ThreadPool::runInThread, this), name_+id));
4545
threads_[i].start();

‎muduo/base/ThreadPool.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ThreadPool : boost::noncopyable
2525
public:
2626
typedef boost::function<void ()> Task;
2727

28-
explicit ThreadPool(const string& name = string());
28+
explicit ThreadPool(const string& name = string("ThreadPool"));
2929
~ThreadPool();
3030

3131
// Must be called before start().

‎muduo/net/EventLoopThread.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using namespace muduo::net;
1919
EventLoopThread::EventLoopThread(const ThreadInitCallback& cb)
2020
: loop_(NULL),
2121
exiting_(false),
22-
thread_(boost::bind(&EventLoopThread::threadFunc, this)),
22+
thread_(boost::bind(&EventLoopThread::threadFunc, this), "EventLoopThread"), // FIXME: number it
2323
mutex_(),
2424
cond_(mutex_),
2525
callback_(cb)

0 commit comments

Comments
 (0)