qfedu-linux-advanced-level/day6/thread3.c

37 lines
676 B
C
Raw Normal View History

2023-08-21 19:45:29 +08:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int n;
void *task1(void *data)
{
n += *((int *)data);
printf("子线程(%ld) n=%d\n", pthread_self(), n);
getchar();
}
int main()
{
pthread_t tid1, tid2;
// 子线程成功之后,则会自动启动线程
int m = 90;
int ret1 = pthread_create(&tid1, NULL, task1, &m);
if (ret1 != 0)
{
perror("pthread_create");
}
int m2 = 100;
int ret2 = pthread_create(&tid2, NULL, task1, &m2);
if (ret2 != 0)
{
perror("pthread_create");
}
printf("主线程(%ld)退出 n=%d\n", pthread_self(), n);
// sleep(3);
getchar();
return 0;
}