|
发表于 2023-1-12 18:34:17
|
显示全部楼层
- sh-5.1$ cat main.c
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- void *task(void *arg) {
- printf("task - arg: %d\n", *(int *)arg);
- printf("task - pid: %x\n", getpid());
- printf("task - tid: %lx\n", pthread_self());
- return arg;
- }
- int main(void) {
- printf("main - pid: %x\n", getpid());
- printf("main - tid: %lx\n", pthread_self());
- pthread_t t0, t1;
- int a = 123, b = 456;
- pthread_create(&t0, NULL, task, &a);
- pthread_create(&t1, NULL, task, &b);
- pthread_join(t0, NULL);
- pthread_join(t1, NULL);
- return 0;
- }
- sh-5.1$ gcc -g -Wall -o main main.c
- sh-5.1$ ./main
- main - pid: aa736
- main - tid: 7f55fc8b9680
- task - arg: 456
- task - pid: aa736
- task - tid: 7f55fbecd6c0
- task - arg: 123
- task - pid: aa736
- task - tid: 7f55fc6ce6c0
- sh-5.1$
复制代码 |
|