-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_name.cpp
More file actions
39 lines (33 loc) · 873 Bytes
/
Copy paththread_name.cpp
File metadata and controls
39 lines (33 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "../include/dispatch_queue/thread_name.hpp"
#include <cstring>
#if defined(_WIN32)
#include <cstdlib>
#include <windows.h>
#include <processthreadsapi.h>
#elif !defined(__EMSCRIPTEN__)
#include <pthread.h>
#endif
namespace dispatch_queue {
template<size_t N>
static void copy_into(char (&buffer)[N], const std::string& s) {
size_t size = std::min(s.size(), N - 1);
memcpy(buffer, s.data(), size);
buffer[size] = '\0';
}
void set_thread_name(const std::string& name) {
#if defined(_WIN32)
wchar_t wcbuf[64];
size_t converted = mbstowcs(wcbuf, name.c_str(), 64);
wcbuf[converted] = L'\0';
SetThreadDescription(GetCurrentThread(), wcbuf);
#elif defined(__APPLE__)
char buf[64];
copy_into(buf, name);
pthread_setname_np(buf);
#elif !defined(__EMSCRIPTEN__)
char buf[16];
copy_into(buf, name);
pthread_setname_np(pthread_self(), buf);
#endif
}
}