add helper method to obtain thread name

this is helpful to, for example, print the name of
the thread that calls a function, i.e.:

log.console("%s from %s\n", __PRETTY_FUNCTION__, thread::get_name().c_str());
This commit is contained in:
Andre Puschmann 2019-10-11 13:26:44 +02:00
parent fd01c44d4e
commit 626259eede
1 changed files with 11 additions and 0 deletions

View File

@ -75,6 +75,17 @@ public:
pthread_cancel(_thread);
}
static std::string get_name()
{
const uint32_t MAX_LEN = 16;
char name[MAX_LEN] = {};
const pthread_t tid = pthread_self();
if (pthread_getname_np(tid, name, MAX_LEN)) {
perror("Could not get pthread name");
}
return std::string(name);
}
protected:
virtual void run_thread() = 0;