46 lines
926 B
C
46 lines
926 B
C
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
// pthread_detach 的使用
|
|
void *task1(void *data)
|
|
{
|
|
// 提供一个整数值,计算它的阶乘
|
|
int n = *((int *)data);
|
|
int *ret = (int *)malloc(sizeof(int));
|
|
*ret = 1;
|
|
while (n)
|
|
{
|
|
*ret *= n--;
|
|
}
|
|
|
|
pthread_exit(ret);
|
|
}
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
if (argc != 3)
|
|
{
|
|
printf("usage: ./%s 数字1 数字2\n", argv[0]);
|
|
}
|
|
|
|
pthread_t tid, tid2;
|
|
int num = atoi(argv[1]);
|
|
pthread_create(&tid, NULL, task1, (void *)&num);
|
|
|
|
int *ret = (int *)malloc(sizeof(int));
|
|
pthread_join(tid, (void **)&ret);
|
|
|
|
int num2 = atoi(argv[2]);
|
|
pthread_create(&tid, NULL, task1, (void *)&num2);
|
|
|
|
int *ret2 = (int *)malloc(sizeof(int));
|
|
pthread_join(tid, (void **)&ret2);
|
|
|
|
printf("%d!*%d! = %d\n", num, num2, (*ret) * (*ret2));
|
|
|
|
sleep(0.5);
|
|
|
|
return 0;
|
|
} |