#include <iostream>
#if defined(__linux__)
#include <sys/syscall.h>
#include <unistd.h>
#endif
//获取当前程序的进程ID
pid_t get_thread_id() {
#if defined(__APPLE__) && defined(__MACH__)
uint64_t tid;
pthread_threadid_np(NULL, &tid);
return (pid_t) tid;
#else
return syscall(SYS_gettid);
#endif
}
int main()
{
std::cout << get_thread_id() << std::endl;
return 0;
}
如何在cpp程序中获取当前进程的进程PID
最新推荐文章于 2025-02-18 00:15:00 发布