#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <string.h> void* thread_function(void* arg) { char* thread_name = (char*)arg; pthread_setname_np(pthread_self(), thread_name); printf("Thread
1#definewtm_set_thread_name(n) ({ \2chartname[THREAD_NAME_LEN +1] =""; \3if(strlen(n) >THREAD_NAME_LEN) \4log_debug("wtm_util_misc","Thread name is too long, truncating it..."); \5strlcpy(tname, n, THREAD_NAME_LEN); \6intret =0; \7if((ret = prctl(PR_SET_NAME, ...
在C/C++中,可以使用pthread_setname_np函数来设置线程名称。以下是一个示例代码: 代码语言:txt 复制 #include <pthread.h> #include <stdio.h> #include <stdlib.h> void* thread_func(void* arg) { const char* thread_name = (const char*)arg; pthread_setname_np(pthread_self(), thread_name); ...
由于prctl的缺点,所以pthread_setname_np()和pthread_getname_np应运而生,能设置指定线程的名称。 函数外(线程外)设置名称std::threadt3(function_3);pthread_setname_np(t3.native_handle(),"t3_thread"); 函数内(线程内)设置名称#include<pthread.h>voidfunction_3(){pthread_setname_np(pthread_self(),...
> #include <pthread.h> #include <unistd.h> void* my_thread(void* arg) { printf("Hello from thread %s!\n", (char*)arg); return NULL; } int main() { pthread_t thread_id; const char* thread_name = "MyThread"; // 设置线程名称 if (pthread_setname_np(thread_id, thread_name)...
在Linux中,线程的名称是通过线程的pthread_setname_np函数来设置的。该函数的原型如下: int pthread_setname_np(pthread_t thread, const char *name); 复制代码 其中,thread参数是要设置名称的线程的标识符,可以通过pthread_self函数获取当前线程的标识符;name参数是要设置的线程名称。 下面是一个示例代码,演示...
#include <sys/prctl.h> //名字的长度最大为15字节,且应该以'\0'结尾 #define set_thread_name(name) prctl(PR_SET_NAME, name, 0, 0, 0); 1. 2. 3. 4. 获取线程名 //char tname[16]; #define get_thread_name(name) prctl(PR_GET_NAME, name) 2....
intset_thread_title(constchar*fmt, ) { chartitle [16]={0}; va_list ap; va_start(ap, fmt); vsnprintf (title,sizeof(title) , fmt, ap); va_end (ap); returnprctl(PR_SET_NAME,title) ; } 现在能够为线程设置名字了,那么如何看到呢 ...
#include <prctl.h> prctl(PR_SET_NAME, "testThread"); // 可以通过设置 PR_GET_NAME 获取当前线程的名字 2.4 示例 需要在线程函数中调用 #include <sys/prctl.h> #include <sys/syscall.h> #include <unistd.h> #include <thread> #include <stdio.h> #include <string.h> #define gettid() sysca...
int retrun; pthread_t nativeHandle; std::thread lowPriorityThread(lowPriorityThreadFunction); nativeHandle = lowPriorityThread.native_handle(); printf("handle:%x\n", nativeHandle); retrun = pthread_setname_np(nativeHandle, "lowPriorityThread"); if (retrun!= 0) { perror("pthread_setname_...