Skip to content

Commit 0fbe68a

Browse files
committed
fix win set worker thread name error
1 parent 43cccf4 commit 0fbe68a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

myframe/common.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,16 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
194194

195195
int Common::SetThreadName(std::thread* t, const std::string& name) {
196196
#if defined(MYFRAME_OS_WINDOWS)
197+
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
198+
if (len <= 0) {
199+
return -1;
200+
}
201+
wchar_t* wide_name = new wchar_t[len];
202+
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
197203
auto handle = t->native_handle();
198-
auto res = SetThreadDescription(handle, name.c_str());
199-
if (res != 0) {
204+
auto res = SetThreadDescription(handle, wide_name);
205+
delete[] wide_name;
206+
if (FAILED(res)) {
200207
return -1;
201208
}
202209
return 0;
@@ -211,9 +218,16 @@ int Common::SetThreadName(std::thread* t, const std::string& name) {
211218

212219
int Common::SetSelfThreadName(const std::string& name) {
213220
#if defined(MYFRAME_OS_WINDOWS)
221+
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
222+
if (len <= 0) {
223+
return -1;
224+
}
225+
wchar_t* wide_name = new wchar_t[len];
226+
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
214227
auto handle = GetCurrentThread();
215-
auto res = SetThreadDescription(handle, name.c_str());
216-
if (res != 0) {
228+
auto res = SetThreadDescription(handle, wide_name);
229+
delete[] wide_name;
230+
if (FAILED(res)) {
217231
return -1;
218232
}
219233
return 0;

myframe/worker_context.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ void WorkerContext::Initialize() {
7878
std::string th_name = mailbox_.Addr();
7979
th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name;
8080
if (Common::SetSelfThreadName(th_name)) {
81-
LOG(WARNING) << "set thread name " << th_name << " failed";
81+
LOG(WARNING) << "set " << mailbox_.Addr()
82+
<< " thread name " << th_name << " failed";
83+
} else {
84+
LOG(INFO) << "set " << mailbox_.Addr()
85+
<< " thread name " << th_name;
8286
}
8387
worker_->Init();
8488
}

0 commit comments

Comments
 (0)