Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

pthread_self() trong C

pthread_self() trả về ID của thread hiện tại. ID duy nhất cho các thread đang chạy. Khi thread kết thúc, ID có thể tái sử dụng.

Ví dụ

#include <stdio.h> #include <pthread.h>

void* func(void* p) {
    printf("Thread ID trong ham: %lu\n", pthread_self());
    pthread_exit(NULL);
}

int main() {
    pthread_t t;
    pthread_create(&t, NULL, func, NULL);
    printf("Thread ID trong main: %lu\n", t);
    pthread_join(t, NULL);
    return 0;
}

Kết quả

Thread ID trong main: 1
Thread ID trong ham: 1