Skip to content

Instantly share code, notes, and snippets.

@npezolano
Last active March 7, 2024 06:46
Show Gist options
  • Save npezolano/5477576 to your computer and use it in GitHub Desktop.
Save npezolano/5477576 to your computer and use it in GitHub Desktop.
Changing the thread name on C++11
#include <thread>
#include <iostream>
#include <pthread.h>
using namespace std;
void foo()
{
string s = "thread1";
char name[16];
pthread_setname_np(pthread_self(), s.c_str()); // set the name (pthread_self() returns the pthread_t of the current thread)
pthread_getname_np(pthread_self(), &name[0], sizeof(name));
cout << name << endl;
}
int main()
{
thread t1(foo);
t1.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment