Skip to content

Commit

Permalink
fix win set worker thread name error
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Jan 3, 2025
1 parent 43cccf4 commit 0fbe68a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 18 additions & 4 deletions myframe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,16 @@ int Common::SetSelfThreadAffinity(int cpu_core) {

int Common::SetThreadName(std::thread* t, const std::string& name) {
#if defined(MYFRAME_OS_WINDOWS)
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
if (len <= 0) {
return -1;
}
wchar_t* wide_name = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
auto handle = t->native_handle();
auto res = SetThreadDescription(handle, name.c_str());
if (res != 0) {
auto res = SetThreadDescription(handle, wide_name);
delete[] wide_name;
if (FAILED(res)) {
return -1;
}
return 0;
Expand All @@ -211,9 +218,16 @@ int Common::SetThreadName(std::thread* t, const std::string& name) {

int Common::SetSelfThreadName(const std::string& name) {
#if defined(MYFRAME_OS_WINDOWS)
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
if (len <= 0) {
return -1;
}
wchar_t* wide_name = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
auto handle = GetCurrentThread();
auto res = SetThreadDescription(handle, name.c_str());
if (res != 0) {
auto res = SetThreadDescription(handle, wide_name);
delete[] wide_name;
if (FAILED(res)) {
return -1;
}
return 0;
Expand Down
6 changes: 5 additions & 1 deletion myframe/worker_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ void WorkerContext::Initialize() {
std::string th_name = mailbox_.Addr();
th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name;
if (Common::SetSelfThreadName(th_name)) {
LOG(WARNING) << "set thread name " << th_name << " failed";
LOG(WARNING) << "set " << mailbox_.Addr()
<< " thread name " << th_name << " failed";
} else {
LOG(INFO) << "set " << mailbox_.Addr()
<< " thread name " << th_name;
}
worker_->Init();
}
Expand Down

0 comments on commit 0fbe68a

Please sign in to comment.