pthread_t 是一个数据类型,在 Linux 多线程编程中,它表示一个线程的标识符
当你使用 POSIX 线程库(pthread)创建一个新线程时,你需要提供一个 pthread_t 类型的变量来存储新线程的标识符。这个标识符可以用于后续的线程操作,例如等待线程结束(pthread_join)或者取消线程(pthread_cancel)等。
下面是一个简单的示例,展示了如何使用 pthread_create 函数创建一个新线程,并将其标识符存储在 pthread_t 类型的变量中:
#include<stdio.h>#include <stdlib.h>#include <pthread.h>void* my_thread(void *arg) { printf("Hello from thread!\n"); return NULL;}int main() { pthread_t thread_id; int rc; rc = pthread_create(&thread_id, NULL, my_thread, NULL); if (rc != 0) { perror("Failed to create thread"); exit(1); } printf("Thread created with ID: %lu\n", (unsigned long)thread_id); pthread_join(thread_id, NULL); return 0;}在这个示例中,我们定义了一个名为 my_thread 的线程函数,然后在 main 函数中创建了一个新线程,并将其标识符存储在 pthread_t 类型的变量 thread_id 中。接着,我们使用 pthread_join 函数等待新线程结束。


