StudyingUnixInterface/11/pthread/thread_id.c

34 lines
596 B
C
Raw Permalink Normal View History

2021-01-11 10:00:36 +00:00
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
pthread_t ntid;
void printids(const char *s) {
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long) pid, (unsigned long) tid, (unsigned long) tid);
}
void *thr_fn(void *arg) {
printids("new thread:");
return (void *)0;
}
int main(void) {
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if(err != 0) {
printf("create thread error\n");
return -1;
}
printids("main thread:");
sleep(1);
return 0;
}